I was wondering if there’s a way to use addition or subtraction on the hex values for color to go up or go down a value.
I tested out a sketch but the colors are limited telling me there’s a loop around with only a specific set of colors.
Here is the code snippet I was testing it on.
int penColor = 0xFFCCFFAA;
color c = #006699;
int grad = 1;
void setup() {
size(1280, 800);
stroke(c);
background(192, 64, 0);
}
void draw() {
line(width/2, height/2, mouseX, mouseY);
println ("Color: " + c);
c += grad;
stroke(c);
/*
if (penColor == 0x000000) {
grad = 1;
}
if (penColor == 0xFFFFFF) {
grad = -1;
}
*/
}
void mousePressed() {
background(192, 64, 0);
}
As the above post might have gotten you to realize, a color is just a number, and the low-bits of that number are the amount of blue in it. So adding 1 constantly to the number makes it change, yes, but it cycles from no blue to full blue before the green values even change by 1! And the red values, being higher bits still, hardly change at all!
You would be much better off if you have three amounts of change - one for red, one for green, and one for blue, that all change at different amounts (0.1, 0.3, 0.5 would be pretty).
You might also look into using colorMode(HSB, ...);
You would be even better of using HSB mode, then you can map say the mouseX position to the hue say and or mouseY to the brightness (I am jumping ahead assuming that you are planning to the mouseX and mouseY for something).
If you are not familiar with hue / saturation / brightness color space, see the end of the Processing color tutorial, and the reference entry for colorMode.
When people think of color math, many think of moving up and down a rainbow – and that is changing hue values at 100% saturation and brightness. Trying to manipulate RGB components simultaneously to achieve similar effects can be unnecessarily hard.
Thank you for the reply. I did some tinkering and this is what I came up with. I will need to test out HSB mode. Is there a good color range pattern or algorithm that goes through all the colors like the rainbow? I assumed going through the colors in hex with be something similar.
int penColor = #000000;
int grad = #010000;
void setup() {
size(1280, 800);
stroke(penColor);
background(192, 64, 0);
randomSeed(0);
}
void draw() {
int r = int(random(0, 5));
switch(r) {
case 0:
grad = #000001;
break;
case 1:
grad = #000010;
break;
case 2:
grad = #000100;
break;
case 3:
grad = #001000;
break;
case 4:
grad = #010000;
break;
case 5:
grad = #100000;
break;
}
penColor += grad;
stroke(penColor);
line(width/2, height/2, mouseX, mouseY);
println ("Color: " + penColor);
}
void mousePressed() {
if (mouseButton == RIGHT)
saveFrame("output.png");
else
background(192, 64, 0);
}