Page 1 of 1

setColor Hexadecimal snippet

Posted: Thu May 31, 2012 9:36 am
by Lafolie
I'm not an amazing programmer by any means, and so I was rather proud when I managed to write this snippet from scratch. It's not particularly useful, but if you come from a web design background and/or you prefer to work with hexes for colours you may find some use for it.

Code: Select all

function ink(hex)
	--convert hexadecimal (accepts 6 or 8 characters)
	local function convertHex(hex)
		local splitToRGB = {}
		
		if # hex < 6 then hex = hex .. string.rep("F", 6 - # hex) end --flesh out bad hexes
		
		for x = 1, # hex - 1, 2 do
			 table.insert(splitToRGB, tonumber(hex:sub(x, x + 1), 16)) --convert hexes to dec
			 if splitToRGB[# splitToRGB] < 0 then slpitToRGB[# splitToRGB] = 0 end --prevents negative values
		end
		return unpack(splitToRGB)
	end
	--predefined colours ("" works for white for convenience)
	if hex == "red" then
		hex = "FF3333FF"
	elseif hex == "green" then
		hex = "33FF33FF"
	elseif hex == "blue" then
		hex = "3333FFFF"
	elseif hex == "white" or hex == "" then
		hex = "FFFFFFFF"
	elseif hex == "black" then
		hex = "333333FF"
	end
	
	love.graphics.setColor(convertHex(hex)) 
end
If you wish to change say, the alpha value over time you can use something like this:

Code: Select all

object.alpha = 0 + increment * dt
object.alpha = string.format("%x", math.floor(v.alpha)) --prepare hex
if # object.alpha == 1 then object.alpha = "0" .. object.alpha end --prefix 0 on low alpha values (below 10). This is due to the nature of %x
ink("ffffff" .. object.alpha)
It adds a little, perhaps unnecessary overhead, but I find it far easier to work with hexes, and eh, I ventured into unknown territory and managed to actually get it working. Feel free to use if you can find any reason too.

Oh, and there's no preset cornflower blue ;)

Re: setColor Hexadecimal snippet

Posted: Thu May 31, 2012 12:17 pm
by coffee
Thank you for the effort. Strangely even doing webdesign I never had real necessity of use/convert hex (in LOVE). So I never add that feature to my custom personal color library/function. Maybe because even dealing daily with hex it's always easy think/keep things decimal. Your code could be however useful someday for mass converting Hex colors.

Re: setColor Hexadecimal snippet

Posted: Thu May 31, 2012 3:26 pm
by Ref
I like litearc's approach for hex color conversions

Code: Select all

hex="ff3333ff"
 _,_,r,g,b,a = hex:find('(%x%x)(%x%x)(%x%x)(%x%x)')
print(r,g,b,a)
color = {tonumber(r,16),tonumber(g,16),tonumber(b,16),tonumber(a,16)}
print(unpack(color))

Re: setColor Hexadecimal snippet

Posted: Fri Jun 01, 2012 10:24 pm
by Lafolie
That's much more elegant! Nice, I shall be looking into that syntax to further my own understanding, thank you.

Re: setColor Hexadecimal snippet

Posted: Thu Sep 27, 2012 3:02 am
by Positive07
If you open the LOVE code then you can add this to setColor function and any other color option just looking for the "#" instead of the RGB code it may be a mess thought but I will look at it later and see what can I do. Thanks for this it looks great, I will try to make something similar just to check something.

EDIT: you should take in care the position in hexadecimal number
#12345678
34=R
56=G
78=B
12=A
and you could also look how long the string is so you can find out if it is RGB or RGBA.

Re: setColor Hexadecimal snippet

Posted: Thu Sep 27, 2012 4:04 am
by awhite92
i use this in my snake game:

Code: Select all

love.graphics.setColor(0xff, 0x66, 0x00)
is the same as #FF6600

[Edit:] and i believe you can just append the alpha the same, but i haven't tested it.

Re: setColor Hexadecimal snippet

Posted: Tue Nov 15, 2022 3:49 pm
by darkfrei
Simple function:

Code: Select all

local function setColorHEX(rgba)
--	setColorHEX(rgba)
--	where rgba = "#336699cc"
	local rb = tonumber(string.sub(rgba, 2, 3), 16) 
	local gb = tonumber(string.sub(rgba, 4, 5), 16) 
	local bb = tonumber(string.sub(rgba, 6, 7), 16)
	local ab = tonumber(string.sub(rgba, 8, 9), 16) or nil
--	print (rb, gb, bb, ab) -- prints 	51	102	153	204
--	print (love.math.colorFromBytes( rb, gb, bb, ab )) -- prints	0.2	0.4	0.6	0.8
	love.graphics.setColor (love.math.colorFromBytes( rb, gb, bb, ab ))
end

Re: setColor Hexadecimal snippet

Posted: Tue Nov 15, 2022 4:40 pm
by milon
darkfrei wrote: Tue Nov 15, 2022 3:49 pm Simple function ...
Ten years is a long time, ya know ;)
Is there a context you'd like to share with the rest of the class?

Re: setColor Hexadecimal snippet

Posted: Tue Nov 15, 2022 4:50 pm
by darkfrei
milon wrote: Tue Nov 15, 2022 4:40 pm
darkfrei wrote: Tue Nov 15, 2022 3:49 pm Simple function ...
Ten years is a long time, ya know ;)
Is there a context you'd like to share with the rest of the class?
The example how to use the color from any color resource as https://imagecolorpicker.com/ (or color palette in hex format):

Code: Select all

setColorHEX("#2596be") -- same as love.graphics.setColor (37/255, 150/25, 190/255)