From RGB values to Int/Color type and HSV to RGB conversion

edited May 2016 in Questions about Code

Hi, strangely i can't seem to find a good answer to both of these problems, and altougth i feel i'm close to the solution using shift operations, i can't do it yet.

I want to pass a color value from arduino to the Processing sketch using serial communication. A the moment i am using the HSV color space on the arduino/fastled, but i can probably convert it to RGB as well. My questions:

  • On the sketch i receive 3 Ints (either RGB or HSV). How can i, from these 3 separate Ints, generate one single Int that can later be requested by the main sketch as a Color datatype? Something like this:

(I can't use << inside the code format, so LEFT SHIFT is actually <<)


rgb_buffer[0] = port.read(); // red value
rgb_buffer[1] = port.read(); // green value
rgb_buffer[2] = port.read(); // blue value

p.println("RGB " + rgb_buffer[0] + ", " + rgb_buffer[1] + ", " + rgb_buffer[2] );

int color_picker = 0;

color_picker = ...; // 0xFF & rgb_buffer[0];
color_picker = ...; // 0xFF & rgb_buffer[1] LEFT SHIFT  8
color_picker = ...; // 0xFF & rgb_buffer[2] LEFT SHIFT  16
  • The other question is, in Processing how can i directly convert a color from RGB to HSV and vice-versa. I know you can use colorMode to switch between modes and ranges, but that doesn't allow me to exactly convert a value from one color space to the other. I'm guessing there is code that i can use for this somewhere inside processing so i was avoiding trying to re-create the wheel.

Answers

  • edited May 2016

    After clearing my head and restarting the search i just found the solution for the first problem, indeed stupidly simple... So i will leave it here:

    
    rgb_buffer[0] = port.read();
    rgb_buffer[1] = port.read();
    rgb_buffer[2] = port.read();
    int r_shifted, b_shifted, g_shifted;
    r_shifted = rgb_buffer[0] LEFT SHIFT 16;
    g_shifted = rgb_buffer[1] LEFT SHIFT  8;
    b_shifted = rgb_buffer[2];
    // mix all channels with 255 for the alpha
    color_picker = 255 LEFT SHIFT 24 | r_shifted | g_shifted | b_shifted;
    

    Still need help on the second question :p

  • edited May 2016

    (I can't use << inside the code format, so LEFT SHIFT is actually <<)

    B/c you're using <pre></pre> in order to post your code.
    A simple raw paste accompanied by CTRL+O is enough for formatting most code in this forum.
    However, if you wanna use <pre></pre> in place of CTRL+O, replace all < w/ &lt;. :P

    https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

  • edited May 2016

    ... in Processing how can i directly convert a color from RGB to HSV and vice-versa?

    I'm afraid Processing (Java Mode) doesn't have HSV. Only RGB & HSB:
    https://Processing.org/reference/colorMode_.html

  • edited May 2016

    Seems like HSV is alias for HSB! b-(
    When in HSB colorMode(), we can convert HSB to RGB directly via color() itself:
    https://Processing.org/reference/color_.html

    Now for RGB to HSB, you may try out java.awt.Color.RGBtoHSB():
    http://docs.Oracle.com/javase/8/docs/api/java/awt/Color.html#RGBtoHSB-int-int-int-float:A-

    Dunno how it exactly works though... 8-X

  • The Java Color class has a number of methods to convert RGB <> HSB but if you don't want to use this class then copy and modify the source code (starts at line 709) into your sketch.

  • I am trying to avoid importing libraries external to Processing to keep the usage and portability to other machines simple, also it doesn't hurt do know how to solve these things by yourself instead of using "black boxes".

    The solution i came up with was using a function from this repository. It works well!

    One of the other problems when doing this conversion were the ranges! Tip for someone else that might be reading this: read the documentation well, in my case the FastLed library on the Arduino only uses 0-255 no matter the color space, then Processing has default values but you can customize them (which i preferred not to change, so i don't introduce bugs myself), and then the code that i posted has yet different ranges from 0-360 and 0.0-1.0.

    When in HSB colorMode(), we can convert HSB to RGB directly via color() itself I didn't see a way to do this conversion directly with color(). You can indeed change the color space and then the color will be interpreted in different ways, but you never really get the converted value from RGB to HSB and vice-versa.

    Thanks again for the help :) Next question should be coming soon, how to speed up serial communication between Processing and Arduino :-/

  • edited May 2016 Answer ✓

    I am trying to avoid importing libraries external to Processing to keep the usage and portability to other machines simple,

    Color class belongs to package java.awt, which comes by default in Java:
    http://docs.Oracle.com/javase/8/docs/api/java/awt/Color.html

    I didn't see a way to do this conversion directly with color().

    Have you seen last example from: https://Processing.org/reference/color_.html ?

    color() always return an int which represents an aRGB value, even when colorMode() is HSB:
    https://Processing.org/reference/color_datatype.html

    In order to trigger that, just pass 3 arguments to it. They're gonna be interpreted according to colorMode(): https://Processing.org/reference/colorMode_.html

    The opposite, aRGB to HSB, is lacking as 1 single function.
    But as I've mentioned before, Processing got hue(), saturation() & brightness() to achieve that.

Sign In or Register to comment.