Get Slightly Lighter and Darker Color from UIColor

前端 未结 19 2384
猫巷女王i
猫巷女王i 2020-12-07 07:08

I was looking to be able to turn any UIColor into a gradient. The way I am intending to do this is by using Core Graphics to draw a gradient. What I am trying to do is to ge

相关标签:
19条回答
  • 2020-12-07 07:23

    Swift universal extension for iOS and OS X, using getHue :

    #if os(OSX)
    
        import Cocoa
        public  typealias PXColor = NSColor
    
        #else
    
        import UIKit
        public  typealias PXColor = UIColor
    
    #endif
    
        extension PXColor {
    
        func lighter(amount : CGFloat = 0.25) -> PXColor {
            return hueColorWithBrightnessAmount(1 + amount)
        }
    
        func darker(amount : CGFloat = 0.25) -> PXColor {
            return hueColorWithBrightnessAmount(1 - amount)
        }
    
        private func hueColorWithBrightnessAmount(amount: CGFloat) -> PXColor {
            var hue         : CGFloat = 0
            var saturation  : CGFloat = 0
            var brightness  : CGFloat = 0
            var alpha       : CGFloat = 0
    
            #if os(iOS)
    
                if getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) {
                    return PXColor( hue: hue,
                                    saturation: saturation,
                                    brightness: brightness * amount,
                                    alpha: alpha )
                } else {
                    return self
                }
    
                #else
    
                getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
                return PXColor( hue: hue,
                                saturation: saturation,
                                brightness: brightness * amount,
                                alpha: alpha )
    
            #endif
    
        }
    
    }
    

    Usage :

    let color = UIColor(red: 0.5, green: 0.8, blue: 0.8, alpha: 1.0)
    color.lighter(amount:0.5)
    color.darker(amount:0.5)
    

    OR (with the default values):

    color.lighter()
    color.darker()
    

    Sample :

    enter image description here

    0 讨论(0)
  • 2020-12-07 07:24

    TL;DR:

    Swift:

    extension UIColor {
    
        var lighterColor: UIColor {
            return lighterColor(removeSaturation: 0.5, resultAlpha: -1)
        }
    
        func lighterColor(removeSaturation val: CGFloat, resultAlpha alpha: CGFloat) -> UIColor {
            var h: CGFloat = 0, s: CGFloat = 0
            var b: CGFloat = 0, a: CGFloat = 0
    
            guard getHue(&h, saturation: &s, brightness: &b, alpha: &a)
                else {return self}
    
            return UIColor(hue: h,
                           saturation: max(s - val, 0.0),
                           brightness: b,
                           alpha: alpha == -1 ? a : alpha)
        }
    }
    

    Usage:

    let lightColor = somethingDark.lighterColor
    

    Objective-C:

    - (UIColor *)lighterColorRemoveSaturation:(CGFloat)removeS
                                  resultAlpha:(CGFloat)alpha {
        CGFloat h,s,b,a;
        if ([self getHue:&h saturation:&s brightness:&b alpha:&a]) {
            return [UIColor colorWithHue:h
                              saturation:MAX(s - removeS, 0.0)
                              brightness:b
                                   alpha:alpha == -1? a:alpha];
        }
        return nil;
    }
    
    - (UIColor *)lighterColor {
        return [self lighterColorRemoveSaturation:0.5
                                      resultAlpha:-1];
    }
    

    @rchampourlier was right in his comment to @user529758 (The accepted answer) - The HSB (Or HSV) and RGB solutions give completely different results. RGB just adds (Or makes the color closer to) white, and the HSB solution brings the color closer to the edge in the Brigtness scale - which basically start with black and ends with the pure color...

    Basically Brightness (Value) makes the color less or more closer to black, where Saturation makes it less or more closer to white...

    As seen here:

    HSV color graph

    So the solution to make a color actually brighter (i.e. closer to white...) will be to make it's Saturation value smaller, resulting this solution:

    - (UIColor *)lighterColor {
        CGFloat h,s,b,a;
        if ([self getHue:&h saturation:&s brightness:&b alpha:&a]) {
            return [UIColor colorWithHue:h
                              saturation:MAX(s - 0.3, 0.0)
                              brightness:b /*MIN(b * 1.3, 1.0)*/
                                   alpha:a];
        }
        return nil;
    }
    
    0 讨论(0)
  • 2020-12-07 07:30

    Swift 5

    extension UIColor {
    
    func lighter(by percentage:CGFloat=30.0) -> UIColor? {
        return self.adjust(by: abs(percentage) )
    }
    
    func darker(by percentage:CGFloat=30.0) -> UIColor? {
        return self.adjust(by: -1 * abs(percentage) )
    }
    
    func adjust(by percentage:CGFloat=30.0) -> UIColor? {
        var r:CGFloat=0, g:CGFloat=0, b:CGFloat=0, a:CGFloat=0;
        if self.getRed(&r, green: &g, blue: &b, alpha: &a) {
            return UIColor(red: min(r + percentage/100, 1.0),
                           green: min(g + percentage/100, 1.0),
                           blue: min(b + percentage/100, 1.0),
                           alpha: a)
        } else {
            return nil
         }
       }
    }
    
    0 讨论(0)
  • 2020-12-07 07:30

    A Swift extension based on @Sebyffffd answer:

    import Foundation
    import UIKit
    
    extension UIColor{
        func colorWith(brightness: CGFloat) -> UIColor{
            var r:CGFloat = 0, g:CGFloat = 0, b:CGFloat = 0, a:CGFloat = 0
    
            if getRed(&r, green: &g, blue: &b, alpha: &a){
                return UIColor(red: max(r + brightness, 0.0), green: max(g + brightness, 0.0), blue: max(b + brightness, 0.0), alpha: a)
            }
    
            return UIColor()
        }
    }
    
    0 讨论(0)
  • 2020-12-07 07:31

    for darker color, this is the simplest: theColor = [theColor shadowWithLevel:s]; //s:0.0 to 1.0

    0 讨论(0)
  • 2020-12-07 07:37

    Here is a UIColor category that also allows control over the amount of color change.

    - (UIColor *)lighterColorWithDelta:(CGFloat)delta
    {
        CGFloat r, g, b, a;
        if ([self getRed:&r green:&g blue:&b alpha:&a])
            return [UIColor colorWithRed:MIN(r + delta, 1.0)
                                   green:MIN(g + delta, 1.0)
                                    blue:MIN(b + delta, 1.0)
                                   alpha:a];
        return nil;
    }
    
    - (UIColor *)darkerColorWithDelta:(CGFloat)delta
    {
        CGFloat r, g, b, a;
        if ([self getRed:&r green:&g blue:&b alpha:&a])
            return [UIColor colorWithRed:MAX(r - delta, 0.0)
                                   green:MAX(g - delta, 0.0)
                                    blue:MAX(b - delta, 0.0)
                                   alpha:a];
        return nil;
    }
    
    0 讨论(0)
提交回复
热议问题