简体   繁体   中英

R or python, gradient of colors ranging from color1 to color2

I'm trying to classify on a scale different microscope color tones for a chemical reaction made on materials. I have the reference colors of both extreme sides of my scale, they're homogeneous in all the extension, so basically I just have RGB codes as starter. Color 1 is white and 2 red, so I have to move between that white and reddish tones, nothing else.

I've read that colorrampPalette could be useful in R, but it works with pre-made colors, I'm just new at this and I try to research but nothing fits at all with what I want, what do you recommend me? thanks

ps also open to work with Python

I don't know if there is such function, but writing one is quite easy:

def gradients(color1, color2, steps):
    # colors are tuples of ints representing RGB values
    r1, g1, b1 = color1  # break color into separate values
    r2, g2, b2 = color2
    step_r = (r2 - r1) / steps  # given n-steps, define step size for each color change
    step_g = (g2 - g1) / steps
    step_b = (b2 - b1) / steps
    return [(int(r1 + step_r * i), # produce list of color applying respective step size over each iteration
             int(g1 + step_g * i), 
             int(b1 + step_b * i)) for i in range(steps)]

Results:

>>> gradients((0,0,0), (255, 255, 255), 3)
>>> [(0, 0, 0), (85, 85, 85), (170, 170, 170)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM