java.awt.Color.RGBtoHSB and java.awt.Color.HSBtoRGB confusion..
in
Programming Questions
•
2 years ago
I'm trying to play around with some HSB pixel manipulation, which requires reading out HSB values from existing pixels.
This functionality isn't built into Processing (yet?)
(see below), but the following topic:
and the Java documentation on the mentioned functions:
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Color.html#RGBtoHSB(int, int, int, float[])
... seemed to be what I'm looking for. However, it's not quite working like I want it to. Copy/paste the following code into a sketch and see for yourself:
EDIT: code is bugged, see response below.
- PImage img;
- img = loadImage(selectInput());
- size(img.width, img.height, P2D);
- loadPixels();
- img.loadPixels();
- for (int i = 0; i < img.pixels.length; ++i) {
- float[] pixelHSB = java.awt.Color.RGBtoHSB(img.pixels[i]>>16, (img.pixels[i]>>8) & 0xFF, img.pixels[i]>>16, null);
- pixels[i] = java.awt.Color.HSBtoRGB(pixelHSB[0], pixelHSB[1], pixelHSB[2]);
- }
- updatePixels();
(obviously, you must open an image file to test)
I figured that the most sensible behaviour would be if the result of RGBtoHSB fed back into HSBtoRGB would result in the same value as what we started with, ignoring rounding errors. How wrong I was...
I've already tried dividing/multiplying by 360.0 (figured the might be inconsistent with the range on Hue), but that's not the answer. Also, I've tried replacing line 8 with:
- colorMode(HSB,1.0);
- pixels[i] = color(pixelHSB[0]/360.0, pixelHSB[1], pixelHSB[2]);
Doesn't solve the problem, and actually produces different results!
If anyone has any idea what is going wrong, please chime in :)
TL;DR: F*cking RGBtoHSB/HSBtoRGB conversions, how do they work?
1