문제

My goal with this algorithm I'm working on is to output a color progression out of some provided colors. By color progression I mean creating the "fade" effect between two colors (color A, color B) and store every color value ((R,G,B) tuple) in between.

For example, if what is provided is total black A = (0,0,0) and total white B = (255,255,255) the progression resultant would be:

P = ((0,0,0),(1,1,1),(2,2,2), .... ,(253,253,253),(254,254,254),(255,255,255)

So that we get white at first and it progressively turns into black. It is, of course, very easy with white and black (just increase RGB by one each step for 255 times). But what if I want to do this procedure with two arbitrary colors, like A = (180,69,1) and B = (233,153,0)??

IMPORTANT NOTE: If with hexadecimal (or any other kind of color notation) it would be easier to achieve, I could work with that too, just specify which type is (take into account that I'm working on PIL (Python Imaging Library), so if it's compatible with that I'm fine)

Obviously it has to be an as even as possible distribution, the progression must be homogeneous.

I need to figure out this algorithm so that I can use it in my Fractal Generator (Mandelbrot Set, google it if you want), so it is important for the progression to be as soft as possible, no hiccups.

Thanks in advance.

도움이 되었습니까?

해결책

Convert your RGB coordinates to HSL or HSV and step through them, converting back to RGB along the way.

다른 팁

I would just interpolate the RGB values independently. See this thread.

I recommend the same question as Jim Clay but read from the top. or see Wikipedia regarding interpolation.

My answer to the SO question titled Range values to pseudocolor might be helpful to you because it shows one way to generate a specific color gradient (the common name of what you referred to as a color progression).

Generally you can interpolate between any two colors in any colorspace by computing the difference, or delta value, between the components of each color, and then divide those by the number of intermediate steps desired to get a fractional delta amount per component to apply after each step.

Then, starting from the value of each of the first color's components, the corresponding fractional amount can be added to it over-and-over to determine each intermediate step color. Alternatively, the arithmetic error inherent in that approach can be reduced by instead adding (step_number/total_steps) * fractional_delta to the initial color component values of the starting color.

I believe this is what @Jim Clay is also saying in his answer. If you'd like some sample code, say so in a comment.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top