hue() returns unpredictable results
in
Programming Questions
•
1 year ago
Hi everyone.
I'm working on a sketch which is about encoding books as sequences of colours where each pixel value corresponds to a single char from the text. So 'a' gives a hue value of 97 for example. In my sketch I set the colorMode(HSB, 255) then populate part of the pixels array with the char values from a string. What's happening though is that when I read the values back out of the pixels array some of them are correct but some aren't. For example a sequence of chars which should read 'projects' reads 'prnjdbtr'. So some characters are correct but some are out by -1. I assume that this must be something to do with the setting for colorMode()? What's very confusing is that if I try the same process in RGB space and extract the red channel it works perfectly. I must be missing something obvious but if anyone could give me a nudge I'd really appreciate it. Code below.
String msg;
boolean useHue=true;
void setup() {
size(500, 500);
//load some text - content isn't important in this context
msg= "It has been a decade since Will Holland aka Quantic released his first album on Tru Thoughts. Twelve albums later and Quantic has a vast discography; funky, soulful vibes with the Quantic Soul Orchestra, Flowering Inferno's reggae infused dub, cumbia with The Combo Barbaro, and more. The Best Of Quantic album highlights the stunning range of his work, bringing together some of the biggest tracks from across his many projects.";
if (useHue) colorMode(HSB, 255);
}
void draw() {
background(0);
int x=0;
int y=0;
//set pixels with char values from the string, the sketch encodes text as a hue values
loadPixels();
for (int i=0;i<msg.length();i++) {
color aColor =color(msg.charAt(i), 255, 255);
pixels[i]=aColor;
}
updatePixels();
x=0;
y=0;
loadPixels();
for (int i=0;i<msg.length();i++) {
//get the colour at this pixel index - this SHOULD be the same as the char value of msg at the same index.
color aColor= pixels[i];
//here's the weird thing - in RGB colour space this works (by extracting the red channel ) but if we use hue then SOME of the char values come out as you'd expect but some have a char value of one less than the should be! eek! any thoughts?
if (useHue) {
println(i+ " value from pixels array: "+(char)hue(aColor)+ " value from original string "+msg.charAt(i));
}
else {
println(i+ " value from pixels array: "+(char)red(aColor)+ " value from original string "+msg.charAt(i));
}
}
updatePixels();
}
2