def rgb(r, g, b):
# convert out of range values for r
if r > 255:
r = 255
elif r < 0:
r = 0
# convert out of range values for g
if g > 255:
g = 255
elif g < 0:
g = 0
# convert out of range values for b
if b > 255:
b = 255
elif b < 0:
b = 0
#Return the hex values
return ('{:02x}{:02x}{:02x}'.format(r,g,b)).upper()
// codewars : RGB To Hex Conversion
function rgb(r, g, b){
// complete this function
return hexC(r)+hexC(g)+hexC(b)
}
function hexC(v){
if(v>255){
v=255
}else if(v<0){
v=0
}
let a=v%16;
let b=Math.floor(v/16);
return hmaps[b]+hmaps[a]
}
let hmaps={
0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'
}
const rgbToHex = (r, g, b) =>
"#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
rgbToHex(0, 51, 255);
// Result: #0033ff
function rgb(r, g, b){
function toHex(a) {
if (a <= 0) return '00';
else if (a >= 255) return 'FF';
else return a.toString(16).toUpperCase();
}
return toHex(r) + toHex(g) + toHex(b);
}
// rgb to hex conversion
fn rgb(r: i32, g: i32, b: i32) -> String {
format!("{:02X}{:02X}{:02X}",
r as f32 as u8,
g as f32 as u8,
b as f32 as u8)
}
fn main() {
let (r, g, b) = (123, 86, 200);
println!("rgb {} {} {} to hex {} ", r, g, b, rgb(r, g, b));
}
return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}",
color.A,
color.R,
color.G,
color.B);