Topic: Calculate RGB values between two colours (Page 1 of 1)

Skaarjj

From: :morF
Insane since: May 2000

posted 10-13-2006 17:44

Okay, say I've got two colour points in the RGB scale, one representing an eye-searing red, the other a mid-level dark green, and I need to calculate 10 evenly-spaced points between them, does anyone know a way I could do this? See, it's part of a java assignment I'm doing, that colours JList item backgrounds dependent on a given value. 12 points in total, including the two extremes. I can do the program, I just don't know how to calculate those points to create the appropaite Color objects for them.

Justice 4 Pat Richard

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted 10-13-2006 18:36

How about just dividing the distance for the channels seperatly by 12, then
adding rdifference / 10 to r, etc for each point?

I don't know if that'l look even though.

Skaarjj

From: :morF
Insane since: May 2000

posted 10-14-2006 01:46

Well, the transition from a mid-dark green also involves dropping the blue channel, since that's what makes the green darker. And since the correct transition goes through green to yellow to orange to red...

Justice 4 Pat Richard

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted 10-14-2006 09:17

In that case, I'm pretty sure RGB won't do for you, you will need to convert into another color space.

Skaarjj

From: :morF
Insane since: May 2000

posted 10-14-2006 09:46

Yes... apparently HSV should be able to do it, but I've still got to work the algorithm out

Justice 4 Pat Richard

(Edited by Skaarjj on 10-14-2006 10:05)

warjournal

From:
Insane since: Aug 2000

posted 10-14-2006 10:35

// starting values
v1x, v1y, v1z
v2x, v2y, v2z

// delta between the xyz axis
dx=v2x-v1x
dy=v2y-v1y
dz=v2z-v1z

// pythagoras to get the distance
distance = sqr( dx*dx + dy*dy + dz*dz )

// get the unit vectors
ux=dx/distance
uy=dy/distance
uz=dz/distance

Once you have the unit vectors and distance, just a matter of dividing, multiplying, and adding it up like TP said.

mult=distance/12.0
for(i==1;i>10;i++){
newx=v1x+ux*i*mult
newy=v1y+uy*i*mult
newz=v1z+uz*i*mult
}

Ahhh... something like that.

Bit of a long way compared to what TP said, but this is how I would go about it. No real reason other than being a freak for vectors. They are just so much fun to play with.

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted 10-14-2006 16:12

Skaarj: HSV <-> RGB conversions are well documented. Check wikipedia: HSV_color_space or wikipedia: HSL_color_space. I doubt using either HSL or HSV would make any difference in the end result.

warjournal: your for(...) statement is borked. It should read: for( i=0; i<10; i++ ) Also there is no need for the 1 add and 1 mul per coordinate. One add is enough. Though it's not as if it would matter with only 10 iterations.

(Edited by poi on 10-14-2006 16:14)

Login Options: Remember Me On This Computer