# How to convert a Double into an Integer?

**URL:** <https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190>\
**Category:** Beginners\
**Created:** [December 6, 2019, 9:22pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190 "2019-12-06T21:22:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [December 6, 2019, 9:22pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/1 "2019-12-06T21:22:51Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 6, 2019, 9:43pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/2 "2019-12-06T21:43:53Z")

</div>

Why don‘t you just cast it like this :

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

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 6, 2019, 10:10pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/3 "2019-12-06T22:10:32Z")

</div>

Hello,

Try this…

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

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4f9d9a26dda8a01168e7f272fb3fd807451e6968.png)

Reference:

> **[How to convert double to int in Java?](https://javarevisited.blogspot.com/2017/01/how-to-convert-double-to-int-in-java.html)**
>
> A blog about Java, Programming, Algorithms, Data Structure, SQL, Linux, Database, Interview questions, and my personal experience.

🙂

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 6, 2019, 11:31pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/4 "2019-12-06T23:31:08Z")

</div>

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 😅

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 7, 2019, 12:26am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/5 "2019-12-07T00:26:29Z")

</div>

```auto
Double someDouble = Math.random() * 0x100;
color c = (color) (double) someDouble;

println(someDouble);
println(c);

exit();

```

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [December 7, 2019, 7:58am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/6 "2019-12-07T07:58:58Z")

</div>

> [@Lexyth](#):
>
> color((int) roo, (int) goo, (int) boo);

–Cannot cast from Double to int

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 7, 2019, 8:05am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/7 "2019-12-07T08:05:52Z")

</div>

> [@paulstgeorge](#):
>
> color( (int) roo, (int) goo, (int) boo );

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

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

```

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [December 7, 2019, 8:06am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/8 "2019-12-07T08:06:58Z")

</div>

> [@glv](#):
>
> int isroo = roo.intValue(); int isgoo = goo.intValue(); int isboo = boo.intValue();

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

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [December 7, 2019, 8:11am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/9 "2019-12-07T08:11:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [December 7, 2019, 8:47am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/10 "2019-12-07T08:47:44Z")

</div>

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

Both work. Is either preferable?

@GoToLoop @glv

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [December 7, 2019, 8:53am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/11 "2019-12-07T08:53:05Z")

</div>

`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.

```auto
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?**

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 7, 2019, 9:31am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/12 "2019-12-07T09:31:15Z")

</div>

if you look  
[https://github.com/neilpanchal/Chroma](https://github.com/neilpanchal/Chroma) ,  
you see example

```auto
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]

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 7, 2019, 10:04am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/13 "2019-12-07T10:04:34Z")

</div>

> [@paulstgeorge](#):
>
> Do you have and use Chroma?

I found it to work with your example:  
[https://github.com/neilpanchal/Chroma](https://github.com/neilpanchal/Chroma)

🙂

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [December 7, 2019, 10:36am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/14 "2019-12-07T10:36:08Z")

</div>

> [@kll](#):
>
> if that works, no need to go long way [UNTESTED]

Ah, but that does not give me anything I can use in  
`img1.pixels[i] = color(isroo, isgoo, isboo);`

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 7, 2019, 10:53am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/15 "2019-12-07T10:53:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [December 7, 2019, 2:25pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/16 "2019-12-07T14:25:13Z")

</div>

> [@kll](#):
>
> did you try and it did not work  
> `img1.pixels[i] = testColor.get();`

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 8, 2019, 12:08am UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/17 "2019-12-08T00:08:28Z")

</div>

@paulstgeorge

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

I am sharing the exercise I did with you.

```auto
// 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() 
  {
  // Jade
  //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)));
  }

```

🙂

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [December 8, 2019, 12:21pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/18 "2019-12-08T12:21:09Z")

</div>

@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.

```auto
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(); 

  // ******************************************************************************
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 8, 2019, 12:33pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/19 "2019-12-08T12:33:21Z")

</div>

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:

```auto
//GLV Template

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

void draw() 
	{
  background(0);
	}

```

Generates this in %temp%

```auto
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.BufferedReader; 
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);
    }
  }
}

```

From [https://github.com/neilpanchal/Chroma](https://github.com/neilpanchal/Chroma) :

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f2a11596213b3d6d258a9146af1efdb48ccb2f39.png)

Thanks for sharing!  
I am not a Chroma user but may find a use for it in future.

🙂

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [December 8, 2019, 1:03pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/20 "2019-12-08T13:03:17Z")

</div>

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?

[Next page](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190.md?page=2)
