|
Author |
Topic: "Color math"? (Read 238 times) |
|
kevinP
|
"Color math"?
« on: Feb 13th, 2004, 9:48pm » |
|
Hi, If I'm storing colors in a color object how can I do "color math" (e.g. "take existing values in c and subtract 10 from R and G")? This might be useful for defining a constant that I can use for modifying all colors... Something like this? (doesn't work) color c = (100, 100, 100); color lighten = (10, 10, 0); c = c - lighten; // now c is (90, 90, 0); -K
|
« Last Edit: Feb 13th, 2004, 9:49pm by kevinP » |
|
Kevin Pfeiffer
|
|
|
bryan Guest
|
Re: "Color math"?
« Reply #1 on: Feb 13th, 2004, 10:54pm » |
|
if you are using the color object it may help to store red green and blue values as seperate variables. int rr = 100; int gg = 100; int bb = 100; int ltn = 10; //lighten value color c = (rr, gg, bb); c = (rr-ltn, gg-ltn, bb-ltn); here you still have a constants ltn and rr gg bb they can always change of course. but for the scope of this script they dont. wonder if this helps?
|
|
|
|
bryan Guest
|
Re: "Color math"?
« Reply #2 on: Feb 13th, 2004, 11:31pm » |
|
i couldnt run the code because im in a computer lab. looks like it should work though. you can add more such as divide and multiply. im not sure how useful those would be though. <CODE> color substract ( color _c1, color _c2 ) { color[] c0 = { _c1, _c2 }; //original color objects float[][] c1 = new float[3][3]; //original color objects split into seperate rgb values for each color, //first dimension third index is for resulting color color c2; for( int i = 0; i < 2; i++ ) { c1[i][0] = red(c0[i]); c1[i][1] = green(c0[i]); c1[i][2] = blue(c0[i]); } c1[2][0] = c1[0][0] - c1[1][0]; c1[2][1] = c1[0][1] - c1[1][1]; c1[2][2] = c1[0][2] - c1[1][2]; return color( c1[2][0], c1[2][1], c1[2][2] ); } color add ( color _c1, color _c2 ) { color[] c0 = { _c1, _c2 }; //original color objects float[][] c1 = new float[3][3]; //original color objects split into seperate rgb values for each color, //first dimension third index is for resulting color color c2; for( int i = 0; i < 2; i++ ) { c1[i][0] = red(c0[i]); c1[i][1] = green(c0[i]); c1[i][2] = blue(c0[i]); } c1[2][0] = c1[0][0] + c1[1][0]; c1[2][1] = c1[0][1] + c1[1][1]; c1[2][2] = c1[0][2] + c1[1][2]; return color( c1[2][0], c1[2][1], c1[2][2] ); } </CODE>
|
|
|
|
kevinP
|
Re: "Color math"?
« Reply #3 on: Feb 14th, 2004, 5:04pm » |
|
Looks good. Don't know why it didn't occur ot me that I could just write these myself (or use your first solution). Txs!
|
Kevin Pfeiffer
|
|
|
|