Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a color in python

Tags:

python

colors

I have a question that may be straight forward, or may be impossible to answer, I'm not sure. I'm wondering how I can define a color in python.

For example, I would like to simply do this:

myColor = #920310

However, using the '#' sign in python automatically comments anything following. Is there a way around this? Thank you and sorry if this question is very simple

like image 804
Sam Creamer Avatar asked Aug 17 '11 15:08

Sam Creamer


2 Answers

If you want it as a string, do

myColor = '#920310'

If you actually want it to be a Color object, you can do something like

myColor = Color('#920310')

and interpret it in Color's constructor.

If the question is can you make # not be interpreted as a comment, then the answer is no. If # wasn't a comment, it wouldn't be Python any more.

You could define your own, Python-like language where a # after an = wouldn't mean a comment (since that's not valid Python anyway) and it wouldn't break any code, but you wouldn't be able to use the # syntax elsewhere without breaking code.

like image 66
agf Avatar answered Sep 24 '22 01:09

agf


myColor = int('920310', 16) #as an integer (assuming an RGB color in hex)

myColor = '#920310' #as a string

from collections import namedtuple
Color = namedtuple("Color", "R G B")
myColor = Color(0x92, 0x03, 0x10)
#as a namedtuple

There are lots of things you could be looking for, and equally many ways to use it. Ultimately, it depends on how you want to use this color.

like image 39
multipleinterfaces Avatar answered Sep 22 '22 01:09

multipleinterfaces