campy.graphics.gcolor module¶
Classes for manipulating colors and pixels.
A GColor
is a heavy object representing an abstract color. It
should be thought of as an opaque data type that can be initialized from any of
several possible representations, and serialized to any of those representations.
A Pixel
represents a single pixel of an image, and is stored much more
efficiently. A Pixel can represent an (R, G, B) triplet or an (R, G, B, A)
quadruplet.
The main difference between a Pixel
and a GColor
is that a
Pixel
is basically an efficiently stored integer representing the alpha,
red, green, and blue channels of the pixel, whereas a GColor
has no
alpha channel and has many more utility methods attached.
Additionally, a Pixel
is mutable, whereas a GColor
is immutable.
-
class
campy.graphics.gcolor.
GColor
(red, green, blue)[source]¶ Bases:
object
A
GColor
represents a color.Colors can be represented in many different forms.
- As a three-integer tuple, such as as (168, 0, 59), with each entry in the range 0..255
- As a three-character hex string prefaced with ‘#’, such as “#83E”
- As a six-character hex string prefaced with ‘#’, such as “#A8003B”
- As a three-character hex string prefaced with ‘0x’, such as “0x83E”
- As a six-character hex string prefaced with ‘0x’, such as “0xA8003B”
- As a case-insensitive name, such as “red” or “LiGhT sKy BlUe”
- As a GColor constant, such as GColor.RED or GColor.LIGHT_SKY_BLUE.
- As a 24-bit integer, representing the red, green, and blue channels.
- As a Pixel object, such as Pixel(168, 0, 59).
The canonical (internal) form for a GColor is as three integers between 0 and 255.
A
GColor
should be thought of as immutable.-
b
¶
-
classmethod
darken
(color)[source]¶ Construct a new color at 2/3 of the current color’s red, green, and blue values, rounded down.
-
g
¶
-
hex
¶
-
name
¶ Return a human-readable name for this color.
-
r
¶
-
rgb
¶
-
class
campy.graphics.gcolor.
Pixel
(red=0, green=0, blue=0, alpha=255)[source]¶ Bases:
object
A
Pixel
represents a single, possibly-transparent pixel from an image.Students will likely never have to directly instantiate Pixels, and will instead likely just modify provided Pixels.
Pixels can be modified directly by getting or setting their properties.
To change the values of channels of a pixel:
cardinal = Pixel(168, 0, 59) print(cardinal.red) # => 168 cardinal.red = 223 cardinal.green = 202 cardinal.blue = 151 red, green, blue = cardinal.rgb()
To modify the alpha channel of a pixel, use the same syntax:
opaque = Pixel(168, 0, 59) opaque.alpha //= 2 alpha, red, green, blue = opaque.argb()
The alpha channel of a pixel defaults to 255 if not otherwise set.
When setting values for these channels, it is required to supply an integer between 0 and 255, inclusive.
-
alpha
¶ Get or set this
Pixel
’s alpha channel.Usage:
pixel = Pixel(168, 0, 59) pixel.alpha = 127 print(pixel.alpha)
-
argb
()[source]¶ Return a 4-tuple of this Pixel’s channels, including alpha transparency.
The elements of the tuple correspond to alpha, red, green, and blue.
To extract all channels at once:
pixel = Pixel(168, 0, 59) alpha, red, green, blue = pixel.argb()
-
blue
¶ Get or set this
Pixel
’s blue channel.Usage:
pixel = Pixel(168, 0, 59) pixel.blue = 41 print(pixel.blue)
-
green
¶ Get or set this
Pixel
’s green channel.Usage:
pixel = Pixel(168, 0, 59) pixel.green = 41 print(pixel.green)
-