Color

class pygamelib.gfx.core.Color(r=0, g=0, b=0)

Bases: pygamelib.base.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.
be_notified(subject[, attribute, value]) A virtual method that needs to be implemented by the observer.
blend(other_color[, fraction]) Blend the color with another one.
detach(observer) Detach an observer from this instance.
from_ansi(string) Create and return a Color object based on an ANSI color string.
load(data) Create a new Color object based on serialized data.
notify([modifier]) Notify all the observers that a change occurred.
randomize() Set a random value for each component
serialize() Serialize a Color into a dictionary.

Attributes

b The b property controls the intensity of the blue color.
g The g property controls the intensity of the green color.
r The r property controls the intensity of the red color.
attach(observer)

Attach an observer to this instance. It means that until it is detached, it will be notified everytime 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)
b

The b property controls the intensity of the blue color. You can set it to an integer between 0 and 255 (both included).

Setting this property triggers a call to notify().

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}")
be_notified(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 paramters 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. This can be None.
  • value (Any) – The new value of the attribute. This can be None.
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.

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)
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 ‘ED;GREEN;BLUEm’ or ‘ED;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 ‘’ and ‘’ will both be parsed into Color(89, 32, 93).

Parameters:string (str) – The ANSI color string to convert.

Example:

color = Color.from_ansi()
g

The g property controls the intensity of the green color. You can set it to an integer between 0 and 255 (both included).

Setting this property triggers a call to notify().

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}")
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:PglInvalidTypeException

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. Two important points:

  1. The “change” that occurred is not specified (but the notifying object is passed as parameter)
  2. No parameters are passed to the be_notified() method except the notifying object.
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()
r

The r property controls the intensity of the red color. You can set it to an integer between 0 and 255 (both included).

Setting this property triggers a call to notify().

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}")
randomize()

Set a random value for each component

This method triggers a call to notify().

Returns:None
Return type:NoneType

Example:

color = Color()
color.randomize()
serialize()

Serialize a Color into a dictionary.

Returns:The class as a dictionary
Return type:dict

Example:

json.dump( color.serialize() )