Is there a better way to cast or convert a Double into an Integer?
I want to put colour values into pixels. To do this I am using color(). The function “color()” expects parameters like “color(int,int,int)”, but the colour information I have is provided as a Double.
“color()” rejects a Double.
So, I start with Double and want to end with Integer.
By trial and error, I have found a tortuous way to do this. I convert the Double to a String and then convert the String to an Integer. See below in “make_image()”.
This works. But is there a better way?
import com.chroma.*; // Import Chroma library
int w = 360;
int h = 360;
PImage img1 = createImage(w, h, RGB);
Chroma testColorLCH; // Declare a chroma object
Chroma testColorRGB; // Declare a chroma object
void setup() {
size(720, 720);
//make_image();
colorMode(RGB, 255,255,255);
noStroke();
}
void draw() {
background(255, 255, 255);
image(img1, (width-w)/2, (height-h)/2);
fill(0, 0, 0);
make_image();
}
void make_image() {
img1.loadPixels();
//
testColorLCH = new Chroma(ColorSpace.LCH, 61, 85, 183, 255);
//convert LCH to RGB
Double roo = testColorLCH.get(ColorSpace.RGB, Channel.R);
Double goo = testColorLCH.get(ColorSpace.RGB, Channel.G);
Double boo = testColorLCH.get(ColorSpace.RGB, Channel.B);
//convert Double to String
String sroo = "" + roo;
String sgoo = "" + goo;
String sboo = "" + boo;
//convert String to Integer
int isroo = int(sroo);
int isgoo = int(sgoo);
int isboo = int(sboo);
//use the integer values in color()
for (int i = 0; i < img1.pixels.length; i++)
img1.pixels[i] = color(isroo, isgoo, isboo);
img1.updatePixels();
//
}