Coordinates
In Processing, the representation of graphic objects is based on a cartesian 3D coordinate system, as displayed in Figure 1.
| Coordinate system |
|---|
![]() |
size() defines the display window size and the
rendering engine that will be used to paint
onto the window. The default engine is JAVA2D, the 2D
graphic Java libray. A bidimensional rendering engine,
especially suitable for faster pixel-based image processing, is P2D
(Processing 2D). If one wants to program in 3D, he must
choose either the P3D (Processing 3D) rendering engine,
especially suited for web-oriented graphics, or OPENGL,
which delegates many typical 3D operations to the graphic
board thus freeing the CPU from many computations. Moreover, if the
objective is high-quality printing with vector graphics, a PDF
rendering option is available.
Images
In Processing, an image can be assigned to an object of the
class PImage. The function
loadImage("myImage") takes a file (gif or jpg)
myImage, containing the pixel coding of an image,
and gives back the content of such image, which can be
assigned to a variable of type PImage. The file
myImage must be loaded in the data
folder of the directory having the same name as the Processing
sketch we are working at.
Note:
New
command is executed, processing opens up a folder named
sketch_??????? within the Processing
directory, corresponding to the name assigned bye the system
to the newly created file. Such folder is accessible from the
Processing menu item Sketch/Add File.PImage gives access, by the fields
width and height, to the width and
height of the loaded image. The image content is accessed via
the pixels[] field.
Example 1: Loading and visualizing an image
size(400,300);
PImage b;
b = loadImage("gondoliers.jpg");
println("width=" + b.width + " height=" + b.height);
image(b, 0, 0, 400, 300); // position (0,0); width=400; height=300;
image(b, 20, 10, 100, 80); // position (20,10); width=100; height=80;
Colors
Since our color receptors (cones), each tuned to a wavelength region, are of three kinds, color models are always referred to a three-dimensional space. In additive color models, each of three axes correspond to a base color, and by mixing three colored light beams one can obtain all colors within a gamut volume in the space defined by the three axes. The three base colors can be chosen arbitrarily or, more often, based on the application domain (e.g., color of three phosphors or laser beams). In printing processes, subtractive color models are used, where the starting point is the white surface and primary ink colors are used to subtract color from white.
Note:
In processing color is a primitive type used to
specify colors. It is realized by a 32-bit number, where the
first byte specifies the alpha value, and the other bytes
specify a triple either in the RGB or in the HSB model. The
choice of one model or the other is made by the
colorMode() function. With three bytes, a number
of
The RGB model
Colors are represented by a triple of numbers, each giving the
intensity of the primary colors Red, Green, and Blue. Each
number can be an unsigned integer, thus taking values between
0 and 255, or be expressed as a floating point number between
colorMode(). The RGB model is additive.
HSB Model
Colors are represented by a triple of numbers, the first number giving the Hue, the second giving Saturation, and the third giving the Brightness.
Note:
| Gimp color chooser |
|---|
![]() |
Alpha channel
It is a byte used to blend and interpolate between images, for
example to render transparency. It can be obtained, from a
variable of type color, with the method
alpha(). The alpha channel can be manipulated
with the method blend() of the class
PImage.
Example 2: Loading and visualizing an image with transparency
|
|
In Processing, it is possible to assign a color to a variable of type color by means of the function
color(), and the model can be previously set with colorMode(). The functions
red(), green(), blue(),
hue(), saturation(), and
brightness() allow to move from one model to the other.
colorMode(RGB);
color c1 = color(102, 30,29);
colorMode(HSB);
color c2 = color(hue(c1), saturation(c1), brightness(c1));
colorMode(RGB);
color c3 = color(red(c2), green(c2), blue(c2));
// the variables c1, c2, and c3 contain the coding of the same color
Tinging an image
An image can be tinged with a color and its transparency can
be set by assigning a given value to the alpha channel. For
this purpose, the function tint() can be
used. For example, a blue tone can be assigned to the inlaid
image of Example 1 by just preceding
the second image() command with tint(0,
153, 204, 126) .
Translations, Rotations, and Scale Transformations
Representing Points and Vectors
In computer graphics, points and vectors are represented with the
- Definition 1: homogeneous coordinates
- quadruples of numbers, where the first triple is to be read in the X-Y-Z space, while the fourth number indicates a vector if it takes value 0, or a point if it takes value 1.
Translations
The function
translate() moves an object in the image
window. It takes two or three parameters, being the
displacements along the directions
Rotations
In two dimensions, the function
rotate() is used to rotate objects in the image
window. This is obtained by (left) multiplying the
coordinates of each pixel of the object by a rotation
matrix. Rotations are always specified around the top left
corner of the window (
rotate(PI/3) before the second
image() command in Example 1. In three dimensions, we can use elementary rotations
around the coordinate axes rotateX(),
rotateY(), e rotateZ().
Scale Transformations
The function
scale() allows to expand or contract an object by
multiplication of its point coordinates by a constant. When it
is invoked with two or three parameters, different scalings can
be applied to the three axes.
Typographic Elements
Every tool or language for media manipulation gives the opportunity to work with written words and with their fundamental visual elements: typographic characters.
The aspect of a type has two main components: font and size.
Processing has the class
PFont and the methods loadFont() (to
load a font and assign it to an object of the
PFont class) and textFont() (to
activate a font with a specific size). In order to load a
font, this has to be pre-loaded into the directory
data of the current sketch. The tool Create
Font, accessible from the Tools menu in
Processing, creates the bitmaps of the characters that the
programmer intends to use. The file with the bitmaps is put in
the data directory. After these preliminary
operations, the font can be used to write some text, using the
function text(). With this function, a string of
characters can be put in the 2D or 3D space, possibly
inserting it within a rectangular box. The alignment of
characters in the box is governed by the function
textAlign(). In the default configuration, the
written text can be spatially transformed like any other
object. The color of characters can be set with the usual
fill(), like for any other graphic object.
Example 3: Overlapped text
|
|
Processing allows a tight control of the
spatial occupation of characters and of the distance between
contiguous characters (see Figure 3). The function textWidth()
computes the horizontal extension of a character or a
string. It can be used, together with the exact coordinates
passed to text(), to control the kerning and
the tracking
between characters. The textSize() allows to
redefine the size of characters. The
textLeading() re-defines the distance in pixels
between adjacent text lines. This distance is measured between
the baselines of the strings of
characters. Letters such as "p" or "q" extend below the
baseline for a number of pixels that can be obtained with the
textDescent(). Instead, the
textAscent() gives back the maximum extension
above the baseline (typically, the height of the letter
"d").
| Typeface metrics |
|---|
![]() |



Rappresentazione di Media in Processing (Italian version)





