I have a set of colors specified in RBG and CMYK. I want to mix a certain amount of white (given in percent) to these colors. How can I do this based on the representations ava开发者_C百科ilable? Can someone recommend a package that can help me with this kind of color conversions?
If you have RGB values r, g and b between 0 and 255 and want to mix them with x% white, you can calculate
r' = ((100 - x) * r + x * 255) / 100
g' = ((100 - x) * g + x * 255) / 100
b' = ((100 - x) * b + x * 255) / 100
If you need it to be efficient, there are ways of doing this with efficient bit operations.
Find the difference between the current color and the target color for each color channel. Calculate your percentage of that difference. Add this to the original value, and you're done.
For example, mixing RGB(255,127,0) with RGB(255,255,255) (white) at 75% gives you RGB(255, 223, 191)
- R: 255-255 = 0 * .75 = 0 + 255 = 255
- G: 255 - 127 = 128 * .75 = 96 + 127 = 224
- B: 255 - 0 = 255 * .75 = 191 + 0 = 191)
You could convert your colors to HSL with this lib, then modify the L component and go back to your previous representation, RGB or CMYK.
精彩评论