How to fade color

后端 未结 8 1285
我寻月下人不归
我寻月下人不归 2021-02-06 05:56

I would like to fade the color of a pixel out toward white, but obviously maintain the same color. If I have a pixel (200,120,40), will adding 10 to each value to m

8条回答
  •  无人及你
    2021-02-06 06:28

    As MarkM suggested, HSB (or HSL) is a simple method for doing this, but will not give perfect hue constance. If this is good enough (I assume you want your own method instead of a module) then this page has code for doing it.

    In python it would look like this:

    def rgb_to_hsl(rgb):
        '''
        Converts an rgb (0..255) tuple to hsl
        '''
        r, g, b = rgb
    
        _r = r / 255                     # RGB in percentage
        _g = g / 255
        _b = b / 255
    
        rgbMin = min(_r, _g, _b)    
        rgbMax = max(_r, _g, _b)
        rgbDelta = rgbMax - rgbMin
    
        l = ( rgbMax + rgbMin ) / 2
    
        if rgbDelta == 0:                        #Greyscale
            h = 0
            s = 0
        else:                                    # Chromatic data...
            if l < 0.5: s = rgbDelta / (rgbMax + rgbMin)
            else:       s = rgbDelta / (2 - rgbMax - rgbMin)
    
            deltaR = (((rgbMax - _r) / 6) + rgbDelta/2) / rgbDelta
            deltaG = (((rgbMax - _g) / 6) + rgbDelta/2) / rgbDelta
            deltaB = (((rgbMax - _b) / 6) + rgbDelta/2) / rgbDelta
    
            if   _r == rgbMax: h = deltaB - deltaG
            elif _g == rgbMax: h = 1/3 + deltaR - deltaB
            elif _b == rgbMax: h = 2/3 + deltaG - deltaR
    
            if h < 0: h += 1
            if h > 1: h -= 1
    
        return (h, s, l)
    
    def hsl_to_rgb(hsl):
        '''
        Converts a hsl tuple to rgb(0..255)
        '''
        h, s, l = hsl
        if s == 0:                      #Greyscale
            r = l * 255
            g = l * 255
            b = l * 255
        else:
            if l < 0.5: var_2 = l * (1 + s)
            else:       var_2 = l + s - (s * l)
    
            var_1 = 2 * l - var_2
    
            r = 255 * hue_to_RGB(var_1, var_2, h + 1/3)
            g = 255 * hue_to_RGB(var_1, var_2, h)
            b = 255 * hue_to_RGB(var_1, var_2, h - 1/3)
        return r, g, b   
    
    def hue_to_RGB (v1, v2, vH):
        '''
        Helper for hsl_to_rgb
        '''
        if vH < 0: vH += 1
        if vH > 1: vH -= 1
        if (6 * vH) < 1: return v1 + (v2 - v1) * 6 * vH
        if (2 * vH) < 1: return v2
        if (3 * vH) < 2: return v1 + (v2 - v1) * 6 * (2/3 - vH)
        return v1
    

    Then to brighten:

    def lighten(rgb):
        '''
        Given RGB values, returns the RGB values of the same colour slightly
        brightened (towards white) 
        '''
        h,s, l = rgb_to_hsl(rgb)
        l = min(l+0.1, 1)              #limit to 1
        return hsl_to_rgb((h, s, l))
    

    The benefit of this method is that the increment is a percentage of the total brightness. Modifying this to take the percentage as an input would be trivial.

    You can reverse engineer the mathematical equations form this code, or see HSL to RGB.

提交回复
热议问题