Color
- class pygamelib.gfx.core.Color(r=0, g=0, b=0)
Bases:
PglBaseObject
New in version 1.3.0.
A color represented by red, green and blue (RGB) components. Values are integer between 0 and 255 (both included).
- Parameters:
r (int) – The red component of the color.
g (int) – The green component of the color.
b (int) – The blue component of the color.
Example:
# color is blue color = Color(0, 0, 255) # and now color is pink color.r = 255
- __init__(r=0, g=0, b=0)
Like the object class, this class constructor takes no parameter.
Methods
__init__
([r, g, b])Like the object class, this class constructor takes no parameter.
attach
(observer)Attach an observer to this instance.
blend
(other_color[, fraction])Blend the color with another one.
copy
()Returns a (deep) copy of this color.
detach
(observer)Detach an observer from this instance.
from_ansi
(string)Create and return a Color object based on an ANSI color string.
handle_notification
(subject[, attribute, value])A virtual method that needs to be implemented by the observer.
load
(data)Create a new Color object based on serialized data.
notify
([modifier, attribute, value])Notify all the observers that a change occurred.
random
()Create and return a new random color.
Set a random value for each of the components of an existing color.
Serialize a Color into a dictionary.
store_screen_position
(row, column)Store the screen position of the object.
Attributes
The b property controls the intensity of the blue color.
The g property controls the intensity of the green color.
The r property controls the intensity of the red color.
A property to get/set the screen column.
A property to get/set the screen row.
- attach(observer)
Attach an observer to this instance. It means that until it is detached, it will be notified every time that a notification is issued (usually on changes).
An object cannot add itself to the list of observers (to avoid infinite recursions).
- Parameters:
observer (
PglBaseObject
) – An observer to attach to this object.- Returns:
True or False depending on the success of the operation.
- Return type:
bool
Example:
myboard = Board() screen = Game.instance().screen # screen will be notified of all changes in myboard myboard.attach(screen)
- property b
The b property controls the intensity of the blue color. You can set it to an integer between 0 and 255 (both included).
When this property is set, the observers are notified with the pygamelib.gfx.core.Color.b:changed event. The value of the event is the new value of the property.
Example:
color = Color(128, 128, 0) print(f"Value for b is {color.b}") color.b = 255 print(f"New value for b is {color.b}")
- blend(other_color, fraction=0.5)
Blend the color with another one. Fraction controls the amount of other_color that is included (0 means no inclusion at all).
- Parameters:
other_color (
Color
) – The color to blend with.fraction (float) – The blending modulation factor between 0 and 1.
- Returns:
A new Color object that contains the blended color.
- Return type:
Example:
a = Color(200, 200, 200) b = Color(25, 25, 25) # c is going to be Color(112, 112, 112) c = a.blend(b, 0.5)
- copy()
Returns a (deep) copy of this color.
Example:
red = Color(255, 0, 0) red2 = red.copy()
- detach(observer)
Detach an observer from this instance. If observer is not in the list this returns False.
- Parameters:
observer (
PglBaseObject
) – An observer to detach from this object.- Returns:
True or False depending on the success of the operation.
- Return type:
bool
Example:
# screen will no longer be notified of the changes in myboard. myboard.detach(screen)
- classmethod from_ansi(string)
Create and return a Color object based on an ANSI color string.
Important
The string must be RGB, i.e ‘[38;2;RED;GREEN;BLUEm’ or ‘[48;2;RED;GREEN;BLUEm’ for foreground and background colors. This method will return None if the color string is not RGB. It is also important to understand that Color is independent from the foreground of background, it is just a color. Therefor ‘[38;2;89;32;93m’ and ‘[48;2;89;32;93m’ will both be parsed into Color(89, 32, 93).
- Parameters:
string (str) – The ANSI color string to convert.
Example:
color = Color.from_ansi()
- property g
The g property controls the intensity of the green color. You can set it to an integer between 0 and 255 (both included).
When this property is set, the observers are notified with the pygamelib.gfx.core.Color.g:changed event. The value of the event is the new value of the property.
Example:
color = Color(128, 128, 0) print(f"Value for g is {color.g}") color.g = 255 print(f"New value for g is {color.g}")
- handle_notification(subject, attribute=None, value=None)
A virtual method that needs to be implemented by the observer. By default it does nothing but each observer needs to implement it if something needs to be done when notified.
This method always receive the notifying object as first parameter. The 2 other parameters are optional and can be None.
You can use the attribute and value as you see fit. You are free to consider attribute as an event and value as the event’s value.
- Parameters:
subject (
PglBaseObject
) – The object that has changed.attribute (str) – The attribute that has changed, it is usually a “FQDN style” string. This can be None.
value (Any) – The new value of the attribute. This can be None.
- classmethod load(data)
Create a new Color object based on serialized data.
If data is None, None is returned.
If a color component is missing from data, it is set to 0 (see examples).
Raises an exception if the color components are not integer.
- Parameters:
data (dict) – Data loaded from JSON data (deserialized).
- Returns:
Either a Color object or None if data where empty.
- Return type:
Color
| NoneType- Raise:
Example:
# Loading from parsed JSON data new_color = Color.load(json_parsed_data['default_sprixel']['fg_color']) # Loading from incomplete data color = Color.load({'red':25,'green':35}) # Result in the following Color object: # Color(25, 35, 0)
- notify(modifier=None, attribute: str = None, value: Any = None) None
Notify all the observers that a change occurred.
- Parameters:
modifier (
PglBaseObject
) – An optional parameter that identify the modifier object to exclude it from the notified objects.attribute (str) – An optional parameter that identify the attribute that has changed.
value (Any) – An optional parameter that identify the new value of the attribute.
Example:
# This example is silly, you would usually notify other objects from inside # an object that changes a value that's important for the observers. color = Color(255,200,125) color.attach(some_text_object) color.notify()
- property r
The r property controls the intensity of the red color. You can set it to an integer between 0 and 255 (both included).
When this property is set, the observers are notified with the pygamelib.gfx.core.Color.r:changed event. The value of the event is the new value of the property.
Example:
color = Color(128, 128, 0) print(f"Value for r is {color.r}") color.r = 255 print(f"New value for r is {color.r}")
- classmethod random()
Create and return a new random color.
- Return type:
Example:
my_color = Color.random()
- randomize()
Set a random value for each of the components of an existing color.
When this method is called, the observers are notified with the pygamelib.gfx.core.Color.randomized event. The value of the event is the new color.
- Returns:
None
- Return type:
NoneType
Example:
color = Color() color.randomize()
- property screen_column: int
A property to get/set the screen column.
- Parameters:
value (int) – the screen column
- Return type:
int
- property screen_row: int
A property to get/set the screen row.
- Parameters:
value (int) – the screen row
- Return type:
int
- serialize()
Serialize a Color into a dictionary.
- Returns:
The class as a dictionary
- Return type:
dict
Example:
json.dump( color.serialize() )
- store_screen_position(row: int, column: int) bool
Store the screen position of the object.
This method is automatically called by Screen.place().
- Parameters:
row (int) – The row (or y) coordinate.
column (int) – The column (or x) coordinate.
Example:
an_object.store_screen_coordinate(3,8)