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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With