Created
June 3, 2016 11:36
-
-
Save tomstuder/45fc62023a660a2a2b1dede2c8727977 to your computer and use it in GitHub Desktop.
How can i convert rgb to hex in p5?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
The p5 code above doesn't work... or rather, it did... then it didn't on some colours.
Use thise.
myColor.toString('#rrggbb')
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
Or, if you just want to be silly:
var hx='#'+('000000'+(r<<16|g<<8|b).toString(16)).slice(-6); // r, g, and b are ints 0..255