# 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:** 3\
**Page:** 2

<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, 2:16pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/21 "2019-12-08T14:16:45Z")

</div>

Hi @paulstgeorge

I was just exploring:

- Chroma library
- Double vs double along with conversion to int
- -835

> [@paulstgeorge](#):
>
> I assume the unexpected -835 for Red is because it is out of gamut.

- how to convert to RGB so it could be used to fill()
- seeing the hex version helped to understand this.

Split it into functions that run independently and do not share common variables:

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

// https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

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);
  
  // Creates a magenta test color
  //testColorLCH = new Chroma(ColorSpace.LCH, 50, 70, 340, 360); 
  // Creates a jade test color
  testColorLCH = new Chroma(ColorSpace.LCH, 61, 85, 183, 255);
  
  make1(testColorLCH);
  make2(testColorLCH);
  make3(testColorLCH);
  make4();
  make5(); //Testing constraints for RGB color with -ve and values over 255
  }

public void make1(Chroma chromaCol) 
  {
  Double roo = chromaCol.get(ColorSpace.RGB, Channel.R);  
  Double goo = chromaCol.get(ColorSpace.RGB, Channel.G);
  Double boo = chromaCol.get(ColorSpace.RGB, Channel.B);

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

  //declare here so scope is the function and not lost in if()
  String sroo, sgoo, sboo;
  int isroo, isgoo, isboo;
  
  if (true) //Try with false
    {
    //convert Double to String
    sroo = "" + roo;
    sgoo = "" + goo;
    sboo = "" + boo;
  
    //convert String to Integer
    isroo = int(sroo);
    isgoo = int(sgoo);
    isboo = int(sboo);
    }
  else
    // This replaces above for this example with Double (capital D)
    {
    isroo = roo.intValue();
    isgoo = goo.intValue();
    isboo = boo.intValue();
    }

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

void make2(Chroma chromaCol) 
  {
  double roo = chromaCol.get(ColorSpace.RGB, Channel.R);  
  double goo = chromaCol.get(ColorSpace.RGB, Channel.G);
  double boo = chromaCol.get(ColorSpace.RGB, Channel.B);  
  
  int r = (int) roo;
  int g = (int) goo;
  int b = (int) boo;
  
  println("Circle 2:");
  println(r, g, b);
  println(hex(color(testColorLCH.get())));
  println(hex(r)); 
  println(hex(g)); 
  println(hex(b));
  println();  
  
  fill(color(r, g, b));
  circle(3*width/4, height/4, 300);
  fill(0);
  text("2", 3*width/4, height/4);
  }
  
// ******************************************************************************   

void make3(Chroma chromaCol) 
  {
  int col = chromaCol.get();
  
  int hexRGB = color(testColorLCH.get());
  int r = (hexRGB&0x00FFFFFF)<<8>>>24; // >>> is an unsigned shift! Try it with >> to see what happens!
  int g = (hexRGB&0x00FFFFFF)<<16>>>24;
  int b = (hexRGB&0x00FFFFFF)<<24>>>24;
  
  //r = (hexRGB & 0x00FF0000)>>16;
  //g = (hexRGB & 0x0000FF00)>>8;
  //b = (hexRGB & 0x000000FF)>>0; 
  
  println("Circle 3:");
  println(r, g, b);
  println(hex(col));
  println(hex(r)); 
  println(hex(g)); 
  println(hex(b));
  println();
  
  fill(color(r, g, b));
  circle(width/4, 3*height/4, 300);
  fill(0);
  text("3", width/4, 3*height/4);
  }

// ******************************************************************************   
  
void make4()
  {
  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);
  }
   
void make5()
  {
  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)));
  } 

```

I was just tinkering with a new library… you know more than me on this.

🙂

---

<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, 5:16pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/22 "2019-12-08T17:16:24Z")

</div>

Intriguing. What does  
if (true)  
mean||do? If “what” is true?

---

<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, 5:58pm UTC](https://discourse.processing.org/t/how-to-convert-a-double-into-an-integer/16190/23 "2019-12-08T17:58:39Z")

</div>

I was selecting between chunks of code… true **selects** first one and **false** the next one.

[https://processing.org/examples/conditionals1.html](https://processing.org/examples/conditionals1.html)  
[https://processing.org/reference/if.html](https://processing.org/reference/if.html)  
[https://processing.org/reference/else.html](https://processing.org/reference/else.html)

I could have thrown a keypress in there to toggle it; you will have to edit it to see what it does.

I found these useful to check the conversions:  
[http://davidjohnstone.net/pages/lch-lab-colour-gradient-picker](http://davidjohnstone.net/pages/lch-lab-colour-gradient-picker)  
[http://colormine.org/convert/lch-to-rgb](http://colormine.org/convert/lch-to-rgb)

🙂

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