setColor Hexadecimal snippet

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

setColor Hexadecimal snippet

Post 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 ;)
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: setColor Hexadecimal snippet

Post 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.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: setColor Hexadecimal snippet

Post 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))
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: setColor Hexadecimal snippet

Post by Lafolie »

That's much more elegant! Nice, I shall be looking into that syntax to further my own understanding, thank you.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: setColor Hexadecimal snippet

Post 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.
Last edited by Positive07 on Sun Sep 30, 2012 4:07 pm, edited 1 time in total.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
awhite92
Prole
Posts: 21
Joined: Thu Jul 26, 2012 4:25 am

Re: setColor Hexadecimal snippet

Post 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.
saved by a scholarship..
redesigned my "avatar"
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: setColor Hexadecimal snippet

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: setColor Hexadecimal snippet

Post 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?
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: setColor Hexadecimal snippet

Post 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)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests