Programmatically Lighten a Color

2020-01-24 03:26发布

Motivation

I'd like to find a way to take an arbitrary color and lighten it a few shades, so that I can programatically create a nice gradient from the one color to a lighter version. The gradient will be used as a background in a UI.

Possibility 1

Obviously I can just split out the RGB values and increase them individually by a certain amount. Is this actually what I want?

Possibility 2

My second thought was to convert the RGB to HSV/HSB/HSL (Hue, Saturation, Value/Brightness/Lightness), increase the brightness a bit, decrease the saturation a bit, and then convert it back to RGB. Will this have the desired effect in general?

20条回答
放我归山
2楼-- · 2020-01-24 04:12

Technically, I don't think either is correct, but I believe you want a variant of option #2. The problem being that taken RGB 990000 and "lightening" it would really just add onto the Red channel (Value, Brightness, Lightness) until you got to FF. After that (solid red), it would be taking down the saturation to go all the way to solid white.

The conversions get annoying, especially since you can't go direct to and from RGB and Lab, but I think you really want to separate the chrominance and luminence values, and just modify the luminence to really achieve what you want.

查看更多
混吃等死
3楼-- · 2020-01-24 04:12

Here's an example of lightening an RGB colour in Python:

def lighten(hex, amount):
    """ Lighten an RGB color by an amount (between 0 and 1),

    e.g. lighten('#4290e5', .5) = #C1FFFF
    """
    hex = hex.replace('#','')
    red = min(255, int(hex[0:2], 16) + 255 * amount)
    green = min(255, int(hex[2:4], 16) + 255 * amount)
    blue = min(255, int(hex[4:6], 16) + 255 * amount)
    return "#%X%X%X" % (int(red), int(green), int(blue))
查看更多
太酷不给撩
4楼-- · 2020-01-24 04:14

I would go for the second option. Generally speaking the RGB space is not really good for doing color manipulation (creating transition from one color to an other, lightening / darkening a color, etc). Below are two sites I've found with a quick search to convert from/to RGB to/from HSL:

查看更多
该账号已被封号
5楼-- · 2020-01-24 04:14

A very similar question, with useful answers, was asked previously: How do I determine darker or lighter color variant of a given color?

Short answer: multiply the RGB values by a constant if you just need "good enough", translate to HSV if you require accuracy.

查看更多
beautiful°
6楼-- · 2020-01-24 04:15

I used Andrew's answer and Mark's answer to make this (as of 1/2013 no range input for ff).

function calcLightness(l, r, g, b) {
    var tmp_r = r;
    var tmp_g = g;
    var tmp_b = b;

    tmp_r = (255 - r) * l + r;
    tmp_g = (255 - g) * l + g;
    tmp_b = (255 - b) * l + b;

    if (tmp_r > 255 || tmp_g > 255 || tmp_b > 255) 
        return { r: r, g: g, b: b };
    else 
        return { r:parseInt(tmp_r), g:parseInt(tmp_g), b:parseInt(tmp_b) }
}

enter image description here

查看更多
祖国的老花朵
7楼-- · 2020-01-24 04:18

ControlPaint class in System.Windows.Forms namespace has static methods Light and Dark:

public static Color Dark(Color baseColor, float percOfDarkDark);

These methods use private implementation of HLSColor. I wish this struct was public and in System.Drawing.

Alternatively, you can use GetHue, GetSaturation, GetBrightness on Color struct to get HSB components. Unfortunately, I didn't find the reverse conversion.

查看更多
登录 后发表回答