We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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:
(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
Answers
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:
Still need help on the second question :p
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/<
. :Phttps://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
I'm afraid Processing (Java Mode)
doesn't have HSV. Only RGB & HSB:https://Processing.org/reference/colorMode_.html
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
Almost forgot: Processing got functions hue(), saturation() and brightness(): O:-)
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.
Thanks again for the help :) Next question should be coming soon, how to speed up serial communication between Processing and Arduino :-/
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
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.