How to convert a Double into an Integer?

Hi @paulstgeorge

I was just exploring:

  • Chroma library
  • Double vs double along with conversion to int
  • -835
  • 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:

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

:slight_smile:

1 Like

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

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/reference/if.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://colormine.org/convert/lch-to-rgb

:slight_smile: