Class RGB

import introcs

This class represents a RGB (with optional alpha) color value. It is the core color value, and the one most supported by graphics packages. In particular, it has conversion methods for tkinter, kivy and PIL.

Constructor

class introcs.RGB(r, g, b, a=255)

An instance is a RGB color value.

All color value ranges are inclusive. So 255 is a valid red value, but 256 is not.

Attributes

RGB.red

The red channel.

Invariant: Value must be an int between 0 and 255, inclusive.

RGB.green

The green channel.

Invariant: Value must be an int between 0 and 255, inclusive.

RGB.blue

The blue channel.

Invariant: Value must be an int between 0 and 255, inclusive.

RGB.alpha

The alpha channel.

This value is used for transparency effects (but not always supported).

Invariant: Value must be an int between 0 and 255, inclusive.

Methods

RGB.glColor()

Converts this color to an OpenGL value.

This conversion allows this object to be used by graphics libraries that depend on OpenGL (e.g. Kivy)

Returns

a 4 element list of the attributes in the range 0 to 1

Return type

list

RGB.webColor()

Converts this color to a web color string.

This conversion allows this object to be used by graphics libraries that depend on Tkinter (e.g. the drawing turtle). The color will not contain alpha (nor will it premulitply any existing alpha) and will be a string in web form.

Returns

a string representing a web color

Return type

str

RGB.rgba()

Converts this color to an rgba value.

This conversion allows this object to be used by graphics libraries that want integer color representation like PIL

Returns

a 4 element tuple of the attributes in the range 0 to 255

Return type

tuple

Class Methods

Class methods are methods that are called with the class name before the period, instead of an object. They provide alternate constructors.

classmethod RGB.CreateName(name)

Creates a new RGB object with the given color name.

Color name conversion is handled by the standard RGB color space. If the color is not valid, this method will fire an assert.

Parameters

name (str) – the color name

Raise

ValueError if name is not a valid color name.

Returns

a new RGB value

classmethod RGB.CreateWebColor(color)

Creates a new RGB object from the given web color string.

A web color string is a 6-digit hexadecimal string starting with a hashtag (#). It does not include an alpha value. If the string is not , this method will fire an assert.

Parameters

color (hexadecimal str) – the web color

Returns

a new RGB value