colors module
Posted: Tue Aug 19, 2014 1:06 am
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.
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.
Of course, you don't have to use these constants, as mentioned above. You can also make your own colors:
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)