drawable¶
-
class
cbc.drawable.
Drawable
(width, height, fill=0)¶ Represents a two-dimensional drawing surface composed of RGBA pixels. A drawable contains
pixels
which holds a list of rows, each of which is a list ofColor
instances (each of which represents a single pixel). For example, a 3x3 drawable looks like this:>>> d = cbc.Drawable(3, 3) >>> d.width 3 >>> d.height 3 >>> d.pixels [[Color('#000000'), Color('#000000'), Color('#000000')], [Color('#000000'), Color('#000000'), Color('#000000')], [Color('#000000'), Color('#000000'), Color('#000000')]]
Internally, the drawable actually represents things more efficiently (a fixed array of 32-bit integers holding the RGBA data), but the Python interface emulates a list of lists of
Color
instances. Methods are provided to get and set pixels individually:>>> d.get(1, 0) Color('#000000') >>> d.set(1, 0, cbc.Color('red')) >>> d.get(1, 0) Color('#660000') >>> d.pixels [[Color('#000000'), Color('#660000'), Color('#000000')], [Color('#000000'), Color('#000000'), Color('#000000')], [Color('#000000'), Color('#000000'), Color('#000000')]]
Alternatively, you can address
pixels
directly, but be aware when doing so that the first coordinate selects the row (the “y” coordinate):>>> d[1, 0] Color('#000000') >>> d[0, 1] Color('#660000') >>> d[0, 1] = cbc.Color('green') >>> d[0, 1] Color('#002200')
You can also select and assign to slices of
pixels
to manipulate whole ranges at once:>>> d.pixels[:, 0] = [cbc.Color('red')] * 3 >>> d.pixels[:, 1] = [cbc.Color('white')] * 3 >>> d.pixels[:, 2] = [cbc.Color('blue')] * 3 >>> d.pixels [[Color('#660000'), Color('#ffffff'), Color('#000066')], [Color('#660000'), Color('#ffffff'), Color('#000066')], [Color('#660000'), Color('#ffffff'), Color('#000066')]] >>> d.pixels[:2, :] = [[cbc.Color('cyan')] * 3] * 2 >>> d.pixels [[Color('#002266'), Color('#002266'), Color('#002266')], [Color('#002266'), Color('#002266'), Color('#002266')], [Color('#660000'), Color('#ffffff'), Color('#000066')]]
Finally, the
clear()
method can be used to quickly reset things to black:>>> d.clear() >>> d.pixels [[Color('#000000'), Color('#000000'), Color('#000000')], [Color('#000000'), Color('#000000'), Color('#000000')], [Color('#000000'), Color('#000000'), Color('#000000')]]
-
clear
(c=Color('#000000'))¶ Reset all pixels to black.
-
draw
(x, y, source, blend_mode=None)¶ Draw the source
Drawable
onto the currentDrawable
at coordinates (x, y). The blend_mode controls how the alpha-channel of the source is treated.
-
classmethod
from_string
(width, height, data, colors={' ': 'black', '.': 'darkgray', 'B': 'lightblue', 'C': 'lightcyan', 'G': 'lightgreen', 'M': 'lightmagenta', 'R': 'lightred', 'W': 'white', 'b': 'blue', 'c': 'cyan', 'g': 'green', 'm': 'magenta', 'n': 'brown', 'o': 'orange', 'r': 'red', 'w': 'gray', 'y': 'yellow'})¶ Constructs a
Drawable
from the given data which is a string containing width x height characters. Each character represents aColor
with colors mapping each character to a color.The default color map is as follows:
char
color
char
color
space
black
.
dark gray
r
red
R
light red
g
green
G
light green
o
brown
y
yellow
b
blue
B
light blue
m
magenta
M
light magenta
c
cyan
C
light cyan
w
gray
W
white
Hence, to construct a rainy-cloud for the display, you could use the following:
cloud = cbc.Drawable.from_string( 5, 5, " . . " "....." " b b " " " " b b ") cbc.display.draw(0, 0, cloud)
-
get
(x, y)¶ Return the pixel at (x, y).
-
property
height
¶ Returns the height of the drawable.
-
property
pixels
¶ Represents the pixels of the drawable as a list of list of
Color
instances. The outer list represents rows (in the same way numpy represents two-dimensional arrays), while the inner represents columns. Hence, addressing pixels is done with the y-coordinate followed by the x-coordinate:>>> d = cbc.Drawable(3, 2) >>> d.width 3 >>> d.height 2 >>> d.pixels [[Color('#000000'), Color('#000000'), Color('#000000')], [Color('#000000'), Color('#000000'), Color('#000000')]] >>> len(d) 2 >>> len(d[0]) 3
Slices can be obtained and assigned to in order to rapidly manipulate multiple pixels. Fast methods are used internally to copy between drawables:
>>> source = cbc.Drawable(5, 5) >>> target = cbc.Drawable(5, 5) >>> source.pixels = [[cbc.Color('white')] * 5] * 5 >>> target.pixels[:3, :3] = source.pixels[:3, :3]
-
set
(x, y, c)¶ Set the pixel at (x, y) to the
color.Color
c.
-
property
width
¶ Returns the width of the drawable.
-