Answers for "rgb to hex lua"

Lua
0

rgb to hex lua

-- assumes decimal values of r, g and b but can be easily retrofitted
function rgb_to_hex(r, g, b)
    --%02x: 0 means replace " "s with "0"s, 2 is width, x means hex
	return string.format("#%02x%02x%02x", 
		math.floor(r*255),
		math.floor(g*255),
		math.floor(b*255))
end

rgb_to_hex(0.01, 0.3, 0.1)
-- returns #024c19
Posted by: Guest on August-17-2021
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

Browse Popular Code Answers by Language