views:

364

answers:

3

Hi All

I wonder if anyone could suggest the best way to go from one color to another in a gradual process.

So - I have a text box which I have a text limit on. I'd like to have the text start at white, but be red by the time it gets to the max text limit.

Is there an easy way to do this? I'm unsure really where to start...

+1  A: 

Use HSL or HSV rather than RGB. They have various properties that go from 0 to 100 percent, so you can scale them nicely in code.

Here's the objective c reference.

jvenema
In this case HSL/HSV isn't any more helpful than RGB. And I'm not sure that the OP is really having trouble constructing the actual UIColor. I think mootymoots is looking for a bigger picture answer of how to address the text box situation.
Jon-Eric
Actually, its the color that is my issue - not assigning it to the text. I just need to work out based on the length of the text, what color value that is and set it to the text....
mootymoots
Right, so that's simple. [character index] / [max length] = [percentage to use in HSV calculation]
jvenema
Yupp got ya - perfect thanks.
mootymoots
A: 

You need to convert your end-point colors to a color space where one of the parameters is intensity so you can keep this constant. @jvenema's suggestion is decent. Then treat the other two values as two points in 2-dimensions, and find the correct point along the line connecting them according to the position of your slider. Convert the resulting color back to RGB for display.

You might also try NSColor's blendedColorWithFraction:ofColor:, depending on how it blends, it might do what you want.

ergosys
it needs to be UIColor rather than NS, and UI doesn't have that method :-(
mootymoots
A: 

Do it with simple linear interpolation. Float is for precision.

float dr = (r_end - r_start) / (text_limit*1.0 - 1);
float dg = (g_end - g_start) / (text_limit*1.0 - 1);
float db = (b_end - b_start) / (text_limit*1.0 - 1);
int x = start_x;
for(int i=0; i<text_size; ++i)
{
    PutOutColoredChar(x,start_y , text[i], (int)(r_start+(dr*i)) , ((int)g_start+(dg*i)), (int)(g_start+(db*i)) );
    x += WithOfChar(text[i]);
}

I am not a color expert but this does not used to be ugly for me.

Notinlist