LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2010, 01:41 PM   #1
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
python set_source_rgb set_source_rgba values


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?

E.g. I have this:
Code:
        cr=area.window.cairo_create()
        cr.set_source_rgba(0.5,0.5,0,0.8)
        cr.rectangle(100,100, 100, 100)
        cr.fill()
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.
 
Old 07-04-2010, 02:41 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by arizonagroovejet View Post
...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 ...
Huh ?

Python is a programming language, so why wouldn't you perform the division in it ?
 
Old 07-05-2010, 02:18 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Original Poster
Rep: Reputation: 198Reputation: 198
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 07-05-2010, 03:55 PM   #4
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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)
Hope it's useful.

Last edited by bgeddy; 07-05-2010 at 03:56 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python, find unique values in a tuple or dictionary action_owl Programming 2 05-11-2010 07:16 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
LXer: Review: Programming in Python 3: A Complete Introduction to the Python Language LXer Syndicated Linux News 0 01-26-2009 04:50 AM
gtk.ListStore - python - retrieving the list values? vharishankar Programming 3 11-30-2006 09:15 PM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration