Page 1 of 1

colors module

Posted: Tue Aug 19, 2014 1:06 am
by satoon101
One thing we implemented recently, but have not publicly announced, is the new colors module. Basic colors are stored here as a Color instance (with their respective Red, Green, and Blue values). Methods and functions that take Color instances can be passed these values instead of having to get the value yourself. Also, a neat trick, is that we implemented a __str__ method for these instances that returns their respective hex values to be used in SayText messages in games that have that functionality.

Syntax: Select all

from messages import SayText
from colors import LIGHT_GREEN
from colors import DARK_BLUE
from colors import RED

my_message = SayText(message='{0}This is lightgreen. {1}This is dark blue. {2}This is red.'.format(LIGHT_GREEN, DARK_BLUE, RED))

You can also change the alpha by getting a new instance using the with_alpha method. You "can" change the alpha (as well as the red, green, and blue values) by using the property "a" (r, g, b for the colors), but we ask that you not do that as that will change the value of the constant itself. If you need a different color, please use the Color class to get your own color, and if you need a new alpha (default is 255), please use the with_alpha method, which returns a new instance.

Syntax: Select all

from messages import SayText
from colors import LIGHT_GREEN
from colors import DARK_BLUE
from colors import RED

my_message = SayText(message='{0}This is lightgreen. {1}This is dark blue. {2}This is red.'.format(LIGHT_GREEN.with_alpha(64), DARK_BLUE.with_alpha(128), RED.with_alpha(192))


# or
my_light_green = LIGHT_GREEN.with_alpha(64)
my_dark_blue = DARK_BLUE.with_alpha(128)
my_red = RED.with_alpha(192)
my_message = SayText(message='{0}This is lightgreen. {1}This is dark blue. {2}This is red.'.format(my_light_green, my_dark_blue, my_red))


Of course, you don't have to use these constants, as mentioned above. You can also make your own colors:

Syntax: Select all

from basetypes import Color

my_color = Color(55, 155, 255)

# Change the alpha and return a new instance
my_new_color = my_color.with_alpha(128)

Posted: Tue Aug 19, 2014 3:30 am
by Doldol
Have you considered implementing the basic web color names ? It wouldn't be really needed, but nice to have imo.

Also why is it implementied in C++ and not Python, there isn't really a performance penalty right, wouldn't that have been a little easier? I'm just wondering.

Posted: Tue Aug 19, 2014 7:42 am
by L'In20Cible
Well, the colors themselves are stored into colors.py so implemented in python. If you are refering to the Color type itself, it's a type we wrapped and exposed directly from the engine.

Posted: Tue Aug 19, 2014 12:46 pm
by satoon101
We "could" implement all of those colors, but that seems a bit much. For now, I just implemented some basic ones, but we could certainly add more later.

I originally implemented the __str__ and with_alpha methods on the Python side, but it seemed pointless to do so since it involved sub-classing the Color object that was already being exposed from the C++ side. It just made more sense to extend the existing Color object to implement these changes.
https://github.com/alliedmodders/hl2sdk/blob/css/public/Color.h

Posted: Tue Aug 19, 2014 11:21 pm
by Doldol
Ah okay interesting, thanks for explaining!

Posted: Wed Aug 27, 2014 2:36 am
by satoon101
I would also like to mention that I got the current colors from Omega's K2Tools svn:
https://svn.german-slaughterhouse.de/svn/k2tools/trunk/addons/source-python/data/custom/k2tools/colormsg/cstrike.ini

I was going to mention this in the announcement, but I could not view his svn for the last couple of weeks, so I could not get the link. Thank you Omega.