How do I determine darker or lighter color variant of a given color?

前端 未结 13 687
情话喂你
情话喂你 2020-12-04 07:37

Given a source color of any hue by the system or user, I\'d like a simple algorithm I can use to work out a lighter or darker variants of the selected color. Similar to effe

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 08:24

    I'm guessing you're using RGB with byte values (0 to 255) as that's very common everywhere.

    For brighter, average the RGB values with the RGB of white. Or, to have some control over how much brightening, mix in them in some proportion. Let f vary from 0.0 to 1.0, then:

    Rnew = (1-f)*R + f*255
    Gnew = (1-f)*G + f*255
    Bnew = (1-f)*B + f*255
    

    For darker, use the RGB of black - which, being all zeros, makes the math easier.

    I leave out details such as converting the result back into bytes, which probably you'd want to do.

提交回复
热议问题