easier copy/paste between hex RGB(A) and LÖVE

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
hoistbypetard
Prole
Posts: 26
Joined: Fri May 22, 2020 7:00 pm

easier copy/paste between hex RGB(A) and LÖVE

Post by hoistbypetard »

With the change to color components being (0,1) instead of (0,255), I've found it harder to convert between color pickers or paint programs and LÖVE.

For 10.x, I'd just see #cdcdcd and type {0xcd, 0xcd, 0xcd, 0xff}. I need a calculator for 11.x :-)

That got painful enough that I made a gadget which takes a HTML-style hex color (rgb or rgba) from either the clipboard or standard input and converts it to 4 comma-separated floats between curly braces, ready to be consumed by unpack(), placing it on either the clipboard or standard output depending on where it came from.

Normally I'd do this in python, but pyperclip wasn't working well on my workstation, and the startup time for the interpreter was noticeable enough that I didn't want to fix it, so it's in C++.

It should work on Mac, Linux or Windows. I've used it heavily on Linux, and done a quick test on Windows.

Source code is available here, MIT licensed:

https://git.sr.ht/~tuxpup/love_color_copy

I made a cross-compiled 32-bit Windows build and tested it out a little. That's attached in case anyone wants it. But that's not ideally what I'd distribute for Windows... I just didn't have a machine with visual studio close at hand to build a static version. The attached should be good enough for anyone who's interested to test and see if they care to make a better build :)

If anyone tries it out, please let me know how it works for you. If anyone builds their own, please let me know if the build instructions are reasonably complete.

I hope it saves someone else as much aggravation as it's saved me.
Attachments
love-color-copy-0.1-win32.zip
cross-build for 32-bit windows, built on linux.
(6.79 MiB) Downloaded 333 times
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: easier copy/paste between hex RGB(A) and LÖVE

Post by grump »

Useful, but that's a lot of code for a simple function.

Code: Select all

local function colorFromValue(val)
	local bit = require('bit')
	local r = bit.rshift(bit.band(val, 0xff0000), 16) / 255
	local g = bit.rshift(bit.band(val, 0xff00), 8) / 255
	local b = bit.band(val, 0xff) / 255
	return r, g, b
end

Code: Select all

love.graphics.setColor(colorFromValue(0x123456))
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: easier copy/paste between hex RGB(A) and LÖVE

Post by zorg »

or if you prefer a string input instead, you can do something like this, based on the above code:

Code: Select all

local function colorFromValueString(str)
	local str = str:gsub('0[xX]','') -- get rid of hex prefix if it exists
	local val = tonumber(str, 16) -- base 16 number conversion
	-- from here, same as above
	local bit = require('bit')
	local r = bit.rshift(bit.band(val, 0xff0000), 16) / 255
	local g = bit.rshift(bit.band(val, 0xff00), 8) / 255
	local b = bit.band(val, 0xff) / 255
	return r, g, b
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
hoistbypetard
Prole
Posts: 26
Joined: Fri May 22, 2020 7:00 pm

Re: easier copy/paste between hex RGB(A) and LÖVE

Post by hoistbypetard »

It is. Mostly because the various places I've been copying color codes from either do rgb, rrggbb, rgba, rrggbbaa, sometimes with quotes, sometimes with leading # or not, depending on which program/site I'm using. And because I wanted it to act directly on the clipboard. The way I use it is with a macro that runs the command then pastes.

It gets used a dozen times whenever I make a new level then left alone. But the amount of aggravation it saved me after a few levels + my LOVE 11 upgrade made it feel worth making. Even though it caused me to learn way more about the X11 clipboard system than I ever wanted to.

I do like your shifts better for the actual conversion.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: easier copy/paste between hex RGB(A) and LÖVE

Post by zorg »

While i can appreciate the work put in the project, the hoops required to be done might put people off to build this themselves...
For a tool, it's probably fine.
For a snippet, one could just use this:

Code: Select all

love.system.setClipboardText(colorFromValueString(love.system.getClipboardText()))
or something similar... still only works with hex strings but it's something. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
hoistbypetard
Prole
Posts: 26
Joined: Fri May 22, 2020 7:00 pm

Re: easier copy/paste between hex RGB(A) and LÖVE

Post by hoistbypetard »

zorg wrote: Sun May 24, 2020 6:06 pm While i can appreciate the work put in the project, the hoops required to be done might put people off to build this themselves...
For a tool, it's probably fine.
For a snippet, one could just use this:

Code: Select all

love.system.setClipboardText(colorFromValueString(love.system.getClipboardText()))
or something similar... still only works with hex strings but it's something. :3

I didn't even think of using LÖVE itself! Nice!
Post Reply

Who is online

Users browsing this forum: No registered users and 80 guests