Skip to content

Instantly share code, notes, and snippets.

@isaacbatst
Created April 29, 2021 22:38
Show Gist options
  • Save isaacbatst/7134d6569f6d9cd69c541344c6ba1ad3 to your computer and use it in GitHub Desktop.
Save isaacbatst/7134d6569f6d9cd69c541344c6ba1ad3 to your computer and use it in GitHub Desktop.
function rgb(r, g, b){
// EARLY RETURN (VALIDAR ANTES)
// se for negativo, fica 00 (0)
// se passar de 255, fica FF (255)
let array = [r, g , b];
for(let color in array){
if(array[color] < 0){
array[color] = "00";
} else if (array[color] > 255){
array[color] = 255;
} else if(array[color] < 10){
array[color] = "0" + array[color]
}
//transformar as variáveis em hexadecimal
array[color] = array[color].toString(16);
}
//array = ['ff', 'ff', 'ff']
let hex = array
.join("")
.toUpperCase();
return hex;
// DEC: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16
// HEX: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10
// BIN: 00 01 10 11 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment