ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've recently starting dabbling with python and gtk/cairo bindings. Can anyone point me to a quick and easy way to convert a colour value from hex (e.g. #FF8000) into the format required by the set_source_rgb function?
but I want to specify the colour #FF8000. I can easily get an rgb value for that hex number using Gimp, but that's with values between 0 and 255 and set_source_rgba wants values between 0 and 1. So I have to bring up a calculator and divide the values Gimp gives me by something or other to get the values that set_source_rgba wants
Having to keep converting colours by such a method is very tedious. I'm thinking there must be a better way, but I've no idea what it is.
Python is a programming language, so why wouldn't you perform the division in it ?
Well yeah I could, obviously. But I was wondering whether there was any sort of online conversion tool or something around that would save me the (admittedly minor) effort of writing some code. Or maybe some way the functions accept hex values.
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Rep:
Here's a little module with a function named split_col that takes a 24bit colour value and returns a tuple of three values with the MSB first (highest first). So the hex value 0xFFFFFF would return (1.0,1.0,1.0). I've assumed the conversion works simply like this. Just import this and call it with your original hex string of #FF8000 like this:
Code:
rgb_col=split_col(0xff8800)
and a tuple of the three colours is returned. Here's the module:
Code:
def split_col(full):
""" split_col(full=24 bit color value) return tuple of three colour values being 0-1.
Assume 0xFFFFFF=White=(1,1,1) 0x000000=Black=(0,0,0) """
return ((full >> 16)/255.0,(255 & (full >> 8))/255.0,(255 & full)/255.0)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.