Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
rgb2hex & hex2rbg — Gideros Forum

rgb2hex & hex2rbg

sslivkasslivka Member
edited June 2013 in Code snippets
function rgb2hex1(r, g, b)
	local rgb, i, m, hex, hexABC = r*65536 + g*256 + b, 0, 0, "", "0123456789ABCDEF"
	while rgb > 0 do
		i = i + 1
		rgb, m = math.floor(rgb/16), math.fmod(rgb, 16) + 1
		hex = string.sub(hexABC, m, m) .. hex
	end
	local switch = {[4] = "00" .. hex, [5] = "0" .. hex, [6] = hex}
	return "0x" .. switch[#hex]
end
 
function rgb2hex2(r, g, b)
	return r*65536 + g*256 + b
end
 
function hex2rbg(hex)
	local rgb, d = {}, {65536, 256, 1}
	for i = 1, 3 do
		rgb[i] = math.floor(hex/d[i])
		hex = math.fmod(hex, d[i])
	end
	return rgb[1], rgb[2], rgb[3]
end
 
print(rgb2hex1(163, 54, 90))
application:setBackgroundColor(rgb2hex2(163, 54, 90))
print(hex2rbg(0xA3365A))

Likes: octotod

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • ar2rsawseenar2rsawseen Maintainer
    @ssilvka cool
    Additionally:
    local rgb_to_hsl = function(r, g, b)
    	r = r/255 
    	g = g/255 
    	b = b/255
    	local max = math.max(r, g, b)
    	local min = math.min(r, g, b)
    	local h
    	local s
    	local l = (max + min) / 2
    	if(max == min) then
    		--achromatic
    		h = 0
    		s = 0
    	else
    		local d = max - min
    		if(l > 0.5) then
    			s = d / (2 - max - min)
    		else 
    			s = d / (max + min)
    		end
     
    		if max == r then
    			if g < b then
    				h = (g - b) / d + 6
    			else
    				h = (g - b) / d
    			end
    		elseif max == g then
    			h = (b - r) / d + 2
    		elseif max == b then
    			h = (r - g) / d + 4
    		end
    		h = h / 6
    	end
    	return math.floor(h*360 + 0.5), math.floor(s*100 + 0.5), math.floor(l*100 + 0.5)
    end
     
    local hsl_to_rgb = function(h, s, l)
    	h = h/360
    	s = s/100
    	l = l/100
    	local r, g, b
    	if(s == 0) then
    		--achromatic
    		r = 1
    		g = 1
    		b = l
    	else
    		local q
    		if l < 0.5 then
    			q = l * (1 + s)
    		else
    			q = l + s - l * s
    		end
    		local p = 2 * l - q
    		r = hue_to_rgb(p, q, h + 1/3)
    		g = hue_to_rgb(p, q, h)
    		b = hue_to_rgb(p, q, h - 1/3)
    	end
    	return math.floor(r * 255 + 0.5), math.floor(g * 255 + 0.5), math.floor(b * 255 + 0.5)
    end
     
    local hue_to_rgb = function(p, q, t)
    	if(t < 0) then t = t + 1 end
    	if(t > 1) then t = t - 1 end
    	if(t < 1/6) then return p + (q - p) * 6 * t end
    	if(t < 1/2) then return q end
    	if(t < 2/3) then return p + (q - p) * (2/3 - t) * 6 end
    	return p
    end
     
    local h, s, l = rgb_to_hsl(35,9,34)
    print(h.." "..s.." "..l)

    Likes: sslivka

    +1 -1 (+1 / -0 )Share on Facebook
  • sslivkasslivka Member
    edited June 2013
    @ar2rsawseen, maybe you know where I can get Color Picker component for Gideros?
  • ar2rsawseenar2rsawseen Maintainer
    you mean like provide selecting a color inside app?
    Sorry don't remember seeing anything like that, but if someone would have such component, it would most probably be @OZApps

    Likes: OZApps

    +1 -1 (+1 / -0 )Share on Facebook
  • you mean like provide selecting a color inside app?
    Sorry don't remember seeing anything like that, but if someone would have such component, it would most probably be @OZApps
    I had created one long before adopting Gideros, so it was made in BeerSDK and there was a video on one of the sites (now Posterous has taken it down, the problem with free services, you just do not know when they will take it down, like Geocities, etc in the past)
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • I managed to get the data from Posterous and I found the link to the color picker video, you can see it here

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • john26john26 Maintainer
    I've often wondered why Gideros insists on colours being entered as a single number. OpenGL specifies them as (R,G,B) triplets which is also easier for the user. Behind the scenes Gideros must be unpacking to (R,G,B) anyway...?
Sign In or Register to comment.