Skip to content

Instantly share code, notes, and snippets.

@tomstuder
Created June 3, 2016 11:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tomstuder/45fc62023a660a2a2b1dede2c8727977 to your computer and use it in GitHub Desktop.
How can i convert rgb to hex in p5?
// 15538: Uncaught TypeError: Cannot read property 'toString' of undefined
function setup() {
createCanvas(400,400);
background(100);
}
function draw() {
// var s = second();
// var shex= color(map(s,0,59,0,100));
// var shex2= hex(s,6);
// var test_color = color(255, 204, 0);
// var hexi = String(s);
var c = color('rgb(255, 204, 0)');
fill(c);
rect(10,10,380,300);
textSize(20);
fill(255);
// text(s,width/2-40,height/2);
// text(shex,width/2-40,height/2 + 30);
// text("#"+ shex2,width/2-40,height/2 + 60);
// text(hexi,width/2-40,70);
// text(typeof hexi,width/2-40,90);
text(c,width/2-40,height/2);
text(hex(c,6),width/2-40,height/2+30);
}
@everythingability
Copy link

The p5 code above doesn't work... or rather, it did... then it didn't on some colours.

Use thise.

myColor.toString('#rrggbb')

@furf
Copy link

furf commented Feb 8, 2023

you can rewrite:

  var hx = "#" + hex(r2,2) + hex(g2,2) + hex(b2,2);

as:

  const hx = `#${hex([r2, g2, b2], 2).join('')}`;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment