Answers for "rgb to hex"

11

javascript rgb to hex

function rgbToHex(r, g, b) {
  return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}

function hexToRgb(hex) {
  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  if(result){
      var r= parseInt(result[1], 16);
      var g= parseInt(result[2], 16);
      var b= parseInt(result[3], 16);
      return r+","+g+","+b;//return 23,14,45 -> reformat if needed 
  } 
  return null;
}
console.log(rgbToHex(10, 54, 120)); //#0a3678
console.log(hexToRgb("#0a3678"));//"10,54,120"
Posted by: Guest on August-02-2019
0

hex to rgb function

function hex2RGB(hex){
        const r = parseInt(hex.substring(1, 3), 16);
        const g = parseInt(hex.substring(3, 5), 16);
        const b = parseInt(hex.substring(5, 7), 16);
        return `${r}, ${g}, ${b}`;
}
Posted by: Guest on December-10-2020
0

rgb to hex lua

-- passing a table like {255, 100, 20}

function application:rgbToHex(rgb)
	local hexadecimal = '0X'

	for key, value in pairs(rgb) do
		local hex = ''

		while(value > 0)do
			local index = math.fmod(value, 16) + 1
			value = math.floor(value / 16)
			hex = string.sub('0123456789ABCDEF', index, index) .. hex			
		end

		if(string.len(hex) == 0)then
			hex = '00'

		elseif(string.len(hex) == 1)then
			hex = '0' .. hex
		end

		hexadecimal = hexadecimal .. hex
	end

	return hexadecimal
end
Posted by: Guest on March-19-2020
1

rgb to hex conversion

// 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));
}
Posted by: Guest on August-29-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language