 # How to convert a Double into an Integer?

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.

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() {

//
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();
//
}
``````
1 Like

Why don‘t you just cast it like this :

``````color((int) roo, (int) goo, (int) boo);
``````

Hello,

Try this…

`````` //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);

int isroo = roo.intValue();
int isgoo = goo.intValue();
int isboo = boo.intValue();
``````

Reference: 2 Likes

Indeed, to convert a Double to int, you should use @glv 's way.

Direct Casting ((int) doubl) will only work if the class is double, not Double (Double is a wrapper for the primitive double).

That was my mistake there 1 Like
``````Double someDouble = Math.random() * 0x100;
color c = (color) (double) someDouble;

println(someDouble);
println(c);

exit();
``````
1 Like

–Cannot cast from Double to int

``````Double r=0d;
Double g=0d;
Double b=0d;

color c = color(r.intValue(), g.intValue(), b.intValue());
``````

Super useful. Thank you. I see you have included a patch of green (61, 85, 183). Do you have and use Chroma?

1 Like

Thanks @Lexyth I get to learn about Doubles and doubles, so it’s all good (to twice the normal extent or degree).

1 Like

color c = (color) (double) roo;
int c = roo.intValue();

Both work. Is either preferable?

`color c = (color) (double) someDouble;` //@GoToLoop

This looks like it could be very good indeed, but I have trouble using it.

instead of breaking the colour into three channels.

``````testColorLCH.get(ColorSpace.RGB, Channel.R);
testColorLCH.get(ColorSpace.RGB, Channel.G);
testColorLCH.get(ColorSpace.RGB, Channel.B);
``````

I have tried to use it with
`testColorLCH.get(ColorSpace.RGB);`

When I try
`color c = (color) (double) testColorLCH.get(ColorSpace.RGB);`

I get:
–Cannot cast from double[] to double

–Type mismatch, “double[]” does not match with “java.lang.Double”

Is there a way to convert the double and the array at the same time?

if you look
https://github.com/neilpanchal/Chroma ,
you see example

``````import com.chroma.*; // Import Chroma library
Chroma testColor; // Declare a chroma object

testColor = new Chroma(ColorSpace.LCH, l, c, h, 255);

fill(testColor.get());  // To fetch RGB color, use the getRGB method.
``````

if that works, no need to go long way [UNTESTED]

I found it to work with your example:
https://github.com/neilpanchal/Chroma Ah, but that does not give me anything I can use in
`img1.pixels[i] = color(isroo, isgoo, isboo);`

did you try and it did not work
`img1.pixels[i] = testColor.get();`
or it is not what you want?

Hi @kll
I tried and the truth is, I do not know whether it is working or not.

This works
`img1.pixels[i] = testColorLCH.get();`

But this does not work
`img1.pixels[i] = testColorLCH.get(ColorSpace.RGB);`

Maybe I am using the wrong syntax. What do you think?

I can do this:
`testColorLCH.get(ColorSpace.RGB);`

and then
`img1.pixels[i] = testColorLCH.get();` //

But as the colour of the pixels would look the same whether it was LCH or LCH converted into RGB, I need a way to know more. Is there a way to discriminate?

If I could do something like
`testColorRGB = testColorLCH.get(ColorSpace.RGB);` //Type mismatch

I would then feel confident to do

`img1.pixels[i] = testColorRGB.get(); //`

@paulstgeorge

I was getting a negative number returned for the red value.

I am sharing the exercise I did with you.

``````// https://github.com/neilpanchal/Chroma
// Import Chroma library from Chroma-3

import com.chroma.*;

Chroma testColorLCH; // Declare a chroma object

public void settings() {  size(600, 600); }

public void setup()
{

background(255);
noStroke();
textAlign(CENTER, CENTER);
textSize(48);
make();
}

public void make()
{
//testColorLCH = new Chroma(ColorSpace.LCH, 50, 70, 340, 360);
//Creates a magenta test color
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);

println("Original:");
println(roo, goo, boo);
int colRGB = testColorLCH.get();
println(hex(colRGB));
println();

//convert Double to String
String sroo = "" + roo;
String sgoo = "" + goo;
String sboo = "" + boo;

//convert String to Integer
int isroo = PApplet.parseInt(sroo);
int isgoo = PApplet.parseInt(sgoo);
int isboo = PApplet.parseInt(sboo);

println("Circle 1:");
println(roo, goo, boo);
println(hex(color(testColorLCH.get())));
println(hex(isroo));
println(hex(isgoo));
println(hex(isboo));
println();

fill(color(isroo, isgoo, isboo));
circle(width/4, height/4, 300);
fill(0);
text("1", width/4, height/4);

//******************************************************************************

//convert LCH to RGB
roo = testColorLCH.get(ColorSpace.RGB, Channel.R);
goo = testColorLCH.get(ColorSpace.RGB, Channel.G);
boo = testColorLCH.get(ColorSpace.RGB, Channel.B);

isroo = roo.intValue();
isgoo = goo.intValue();
isboo = boo.intValue();

println("Circle 2:");
println(isroo, isgoo, isboo);
println(hex(color(testColorLCH.get())));
println(hex(isroo));
println(hex(isgoo));
println(hex(isboo));
println();

fill(color(isroo, isgoo, isboo));
circle(3*width/4, height/4, 300);
fill(0);
text("2", 3*width/4, height/4);

//******************************************************************************

int hexRGB = color(testColorLCH.get());
isroo = (hexRGB&0x00FFFFFF)<<8>>>24;
isgoo = (hexRGB&0x00FFFFFF)<<16>>>24;
isboo = (hexRGB&0x00FFFFFF)<<24>>>24;

//isroo = (hexRGB & 0x00FF0000)>>16;
//isgoo = (hexRGB & 0x0000FF00)>>8;
//isboo = (hexRGB & 0x000000FF)>>0;

println("Circle 3:");
println(isroo, isgoo, isboo);
println(hex(colRGB));
println(hex(isroo));
println(hex(isgoo));
println(hex(isboo));
println();

fill(color(isroo, isgoo, isboo));
circle(width/4, 3*height/4, 300);
fill(0);
text("3", width/4, 3*height/4);

//******************************************************************************

println("Circle 4:");
//println(color(0xFF00B399));
println(hex(color(0xFF00B399)));
//println(color(0, 179, 153));
color c = color(0, 179, 153);
println(hex(color(0, 179, 153)));
println(hex(int(red(c))));
println(hex(int(green(c))));
println(hex(int(blue(c))));
println();

fill(c);
circle(3*width/4, 3*height/4, 300);
fill(0);
text("4", 3*width/4, 3*height/4);

println("Test:");
println(hex(-835));
println(hex(color(-835, 179, 153)));
println(hex(color(-1, 179, 153)));
println(hex(color(-2, 179, 153)));
println(hex(color(255, 179, 153)));
println(hex(color(256, 179, 153)));
}
`````` @glv
Excellent! I am going through section by section.

First section (see below). I assume the unexpected -835 for Red is because it is out of gamut.
`println("Valid RGB Color: " + !testColorLCH.clipped());`// false

What is this line for?
`int colRGB = testColorLCH.get();`
colRGB can be changed to colWhatever and it makes no difference to the hex value. It is the hex of the LCH.

``````import com.chroma.*;

Chroma testColorLCH; // Declare a chroma object

public void settings() {
size(600, 600);
} // settings? https://processing.org/reference/settings_.html

public void setup() // why public? https://processing.org/reference/public.html
{

background(255);
noStroke();
textAlign(CENTER, CENTER);
textSize(48);
make();
}

public void make()
{
testColorLCH = new Chroma(ColorSpace.LCH, 61, 85, 183, 255);

println("Valid RGB Color: " + !testColorLCH.clipped());

//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);

println("Original:");
println(roo, goo, boo);
int colRGB = testColorLCH.get(); // what does this do?
println(hex(colRGB));
println();

//******************************************************************************
}
``````

Hi,

I was wanted to see the variables and understand what was happening; I did not like the -835!

The variable name is not relevant.

The -835 appears to be constrained to between 0 and 255; see the last line of code.

Sometimes I get a “Could not run sketch” and have to resurrect it from %temp%.

This:

``````//GLV Template

void setup()
{
size(640, 360);
}

void draw()
{
background(0);
}
``````

Generates this in %temp%

``````import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;

import java.util.HashMap;
import java.util.ArrayList;
import java.io.File;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class sketch_191208b extends PApplet {

//GLV Template

public void setup()
{

}

public void draw()
{
background(0);
}
public void settings() {  size(640, 360); }
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "sketch_191208b" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}

``````

Thanks for sharing!
I am not a Chroma user but may find a use for it in future. Hi @glv
I am looking at the rest of the code, with great interest.
The variables roo, isroo, etc. Are the same variables being given new values? Does Processing read down or up (inside draw())? For example, if in line 70 you have
`isroo = roo.intValue();`
is it the most recent roo that is used (line 66) or the first roo (line 28)?

Also, if one uses
`testColorLCH.get(ColorSpace.RGB);`

Is the colorspace for testColorLCH then RGB until it is changed again?

Also, your tests look very interesting. Please will you share what you were testing and what you learned?