Skip to main content

coloX Graphics API

coloX provides a complete 2D drawing API, including pixel operations, basic shapes, sprites, palette management, and image block operations.


Contents​

  1. Canvas operations
  2. Color system
  3. Pixel operations
  4. Basic shapes
  5. Sprite system
  6. Palette operations
  7. Fill patterns
  8. Image block operations
  9. QR codes
  10. Complete API index

1. Canvas Operations​

cls([col])​

Clears the screen and resets the clipping rectangle and print state. col defaults to 0 (black).

cls()           -- Clear to black
cls(dark_blue) -- Clear to dark blue

camera([x, y]) → old_x, old_y​

Sets the screen offset for all subsequent drawing operations. Actual drawing position = original coordinates - (x, y).

Returns: The previous offset (x, y), useful for restoring a temporary offset.

Call camera() without arguments to restore the default (no offset).

-- Keep the character at the center of the screen
camera(player_x - __vm_width / 2, player_y - __vm_height / 2)
map() -- Move the map with the character
camera() -- Reset so subsequent drawing is unaffected

-- Draw with a temporary offset, then reset
local ox, oy = camera(16, 16)
rect(0, 0, 32, 32)
camera(ox, oy)

clip([x, y, w, h [, clip_previous]]) → old_x, old_y, old_w, old_h​

Sets the clipping rectangle; all drawing operations take effect only within this region.

Returns: The previous clipping region (x, y, w, h), useful for restoring it after a partial redraw.

Parameters:

ParameterDescription
x, yTop-left corner of the clipping region
w, hClipping region width and height
clip_previousIf true, intersect the new and previous clipping regions

Call clip() without arguments to reset to the full screen.

clip(20, 20, 100, 80)         -- Draw only within (20,20)-(120,100)
clip(30, 30, 50, 50, true) -- Intersect with the previous region: (30,30)-(80,80)
clip() -- Reset

flip()​

Immediately presents the content drawn so far (without waiting for the next _draw frame). Useful for progress updates during lengthy operations: inside a loop, cls(), draw progress, then flip().

for i = 1, 100 do
-- ...time-consuming processing...
cls()
print("processing " .. i .. "/100", 40, 110)
flip()
end

2. Color System​

coloX includes 16-color palette constants (see platforms.md). Custom colors are also available in 16-bit/32-bit modes.

color([col]) → old_col​

Sets the current drawing color, used by subsequent drawing calls without a color argument. Defaults to light_gray when no argument is given.

Returns: The previous color.

color(red)
line(0, 0, 100, 100) -- Red line

palrgb(idx, r, g, b, a) → color_value​

Sets the color at a palette index and returns a color value accepted by drawing functions.

local my_color = palrgb(0, 200, 180, 255)
circfill(120, 120, 50, my_color)

getcolor(r, g, b [, a]) → color_value​

Gets a usable color value from RGB. In palette mode, finds the nearest palette color.

local coral = getcolor(0xff, 0x7f, 0x50)
rectfill(10, 10, 100, 100, coral)

3. Pixel Operations​

pset(x, y [, col])​

Sets the pixel color at (x, y). Uses the current drawing color when col is omitted.

for y = 0, __vm_height - 1 do
for x = 0, __vm_width - 1 do
pset(x, y, (x + y) % 16)
end
end

pget(x, y) → color​

Gets the screen pixel color at (x, y). Returns 0 outside the valid range.

-- Pixel copying effect
for y = 0, 120 do
for x = 0, 120 do
pset(x + 120, y, pget(x, y))
end
end

sget(x, y) → color​

Gets the sprite sheet pixel color at (x, y). Returns 0 outside the valid range.

sset(x, y [, col])​

Sets the sprite sheet pixel color at (x, y). Uses the current drawing color when col is omitted (as with pset).


4. Basic Shapes​

line(x0, y0 [, x1, y1 [, col]])​

Draws a line from (x0, y0) to (x1, y1). If the endpoint is omitted, uses the endpoint of the previous line. Calling without arguments resets the endpoint.

-- Draw radial lines
cls()
line()
for i = 0, 6 do
line(120 + cos(i / 6) * 80, 120 + sin(i / 6) * 80, 8 + i)
end

lines(coords [, col [, close]])​

Draws a polyline from a coordinate table. Closes it if close is true.

lines({0, 0, 50, 50, 100, 0}, red, true)  -- Closed triangle

wline(width, x0, y0, x1, y1 [, col])​

Draws a line segment with a specified width.

bezier(coords [, segments [, col [, width]]])​

Draws a Bezier curve. coords is a coordinate table (as in lines/polygonfill) with at least 6 numbers (3 control points, a quadratic curve); 8 numbers (4 control points) make a cubic curve, with up to 63 numbers.

ParameterDefaultDescription
segments2Subdivisions per curve segment; larger values are smoother
colCurrent drawing colorLine color
width1Line width
bezier({10, 100, 120, 10, 230, 100}, 8, white)  -- Quadratic curve
bezier({10, 100, 10, 10, 230, 10, 230, 100}, 8) -- Cubic curve

rect(x0, y0, x1, y1 [, col])​

Draws a rectangle outline.

rectfill(x0, y0, x1, y1 [, col])​

Draws a filled rectangle.

rectfill(10, 20, 100, 80, blue)
rect(10, 20, 100, 80, white) -- Outline

rectcls(x0, y0, x1, y1, col)​

Fills a rectangle (col required). Bypasses palette remapping (pal) and fill patterns (fillp), writing color values directly for fast region clearing; camera/clip still apply.

circ(x, y, r [, col])​

Draws a circle outline.

circfill(x, y, r [, col])​

Draws a filled circle. Nothing is drawn when r is negative.

circfill(120, 120, 60, dark_blue)  -- Filled circle
circ(120, 120, 60, white) -- Circle outline

oval(x0, y0, x1, y1 [, col])​

Draws an ellipse outline inside the rectangle defined by (x0,y0) and (x1,y1).

ovalfill(x0, y0, x1, y1 [, col])​

Draws a filled ellipse.

ovalfill(50, 80, 190, 160, green)  -- Ellipse

arc(x, y, r, angle1, angle2 [, col])​

Draws an arc counterclockwise from angle1 to angle2. Angles range from [-180, 180], with 0° on the right (r, 0) and 90° above (0, -r).

-- Draw a 90-degree arc
arc(120, 120, 50, 0, 90, yellow)

polygonfill(coords [, col])​

Fills a polygon. The coordinate table must contain at least 3 point pairs and at most 64.

polygonfill({120, 50, 170, 150, 70, 150}, red)  -- Triangle

trifill(x0, y0, x1, y1, x2, y2 [, col])​

Fills a triangle.


5. Sprite System​

spr(n, x, y [, w, h [, flip_x, flip_y]])​

Draws sprite n at (x, y), numbered from 0.

Parameters:

ParameterTypeDefaultDescription
nnumberRequiredSprite number (0..n)
x, ynumberRequiredScreen position
w, hnumber1, 1Number of sprite blocks (8×8 pixels each)
flip_xbooleanfalseFlip horizontally
flip_ybooleanfalseFlip vertically

Color 0 is transparent by default (change with palt()).

spr(0, 50, 50)           -- Single sprite
spr(0, 50, 50, 2, 3) -- Combined 2×3 sprite block
spr(0, 50, 50, 1, 1, true, false) -- Flip horizontally

sspr(sx, sy, sw, sh, dx, dy [, dw, dh [, flip_x, flip_y]])​

Stretches a sprite sheet rectangle (sx, sy, sw, sh) into a destination screen rectangle (dx, dy, dw, dh).

-- Stretch sprite-sheet region (0,0)-(16,16) to 64×64 on screen
sspr(0, 0, 16, 16, 80, 80, 64, 64)

rotspr(sx, sy, sw, sh, dx, dy, angle, zoomx, zoomy)​

Similar to sspr, but supports rotation and scaling.

tspr(x0, y0, x1, y1, sprx, spry [, sprdx, sprdy])​

Draws a textured line from (x0,y0) to (x1,y1), sampling the sprite sheet at (sprx, spry). sprdx/sprdy are sampling increments (default 1/8, 0).

fget(n [, f]) → value​

Gets sprite n's flags. f is a flag index 0..7, returning true/false. Omitting f returns a bitfield of all flags.

fset(n [, f], val)​

Sets sprite n's flags. val is true/false or an integer bitfield.

fset(2, 1 | 2 | 8)    -- Set bits 0, 1, 3
fset(2, 4, true) -- Set bit 4
print(fget(2)) -- → 27 (1|2|8|16)

6. Palette Operations​

pal(c0, c1 [, p])​

Color remapping has two modes:

Mode 0 (drawing palette, default): Replaces color c0 with c1 while drawing.

pal(9, 8)        -- Subsequent drawing: orange (9) becomes red (8)
spr(1, 70, 60) -- Orange sprite pixels become red

Mode 1 (display palette): Remaps the whole screen when presenting a frame. Previously drawn content is also affected.

pal({1, 1, 5, 5, 5, 6, 7, 13, 6, 7, 7, 6, 13, 6, 7, 1}, 1)  -- Make the full screen grayscale

In 8bpp specifications, mode 0 is a no-op (drawing remapping is used only for transparency flags managed by palt); only mode 1 applies (full-byte 0-255 remapping).

Table form: Set multiple entries at once.

pal({[12] = 9, [14] = 8})  -- Colors 12→9, 14→8

Returns: For a single-value setting, the previous mapping for that color.

Reset:

pal()    -- Reset all palettes
pal(0) -- Reset the draw palette
pal(1) -- Reset the display palette

palt(c [, t])​

Sets color transparency. t=true makes the color transparent; false makes it opaque.

Default: color 0 is transparent; all others are opaque.

palt(8, true)       -- Make color 8 transparent
palt() -- Reset to defaults
palt(0xc000) -- Bitfield: colors 0 and 1 are transparent

7. Fill Patterns (fillp)​

fillp(p)​

Sets a two-color 4×4 fill pattern, affecting: circ, circfill, rect, rectfill, polygonfill, trifill, oval, ovalfill, pset, line, and lines.

Pattern bitfield:

  .-----------------------.
|32768|16384| 8192| 4096|
|-----|-----|-----|-----|
| 2048| 1024| 512 | 256 |
|-----|-----|-----|-----|
| 128 | 64 | 32 | 16 |
|-----|-----|-----|-----|
| 8 | 4 | 2 | 1 |
'-----------------------'
-- Checkerboard
fillp(4 + 8 + 64 + 128 + 256 + 512 + 4096 + 8192)
circfill(120, 120, 50, 0x4E) -- Alternate brown and pink

Pattern control bits (high fractional bits):

BitDescription
0b0.1Transparent mode: do not draw the pattern's second color
-- Transparent checkerboard
fillp(0b0011001111001100.1)

fillp(p) returns the previous pattern. fillp(0) restores solid fill.


8. Image Block Operations (blit)​

blit(x, y, opflag, data, linebytes, w, h, bpp, flag, sx, sy, sw, sh) → boolean​

Draws a block of image data. Returns true on success.

Parameters:

ParameterDescription
x, yTop-left drawing destination
opflagDrawing strategy (see below)
dataImage data string, or special value "SPR"/"SCR"
linebytesData bytes per row
w, hImage dimensions (pixels)
bppBits per pixel
flagImage properties (see below)
sx, sy, sw, shSelected source image rectangle

opflag bit definitions:

BitDescription
bit01 = Alpha blending (32-bit color mode only)
bit1-2X anchor: 0=left, 1=right, 2=center
bit3-4Y anchor: 0=top, 1=bottom, 2=center

flag bit definitions:

BitDescription
bit0Premultiplied alpha data (32-bit premultiplication)
bit1Swap blue/red data
bit2OPAQUE (disable alpha)

Special data values:

ValueDescription
"SPR"Use sprite sheet data (ignore linebytes/w/h/bpp)
"SCR"Use screen data (ignore linebytes/w/h/bpp)

9. QR Codes​

qrcode(str [, col [, w [, x, y]]]) → raw_width​

Draws a QR code at (x, y). col defaults to the current drawing color.

Parameters:

ParameterDescription
strQR code content string
colForeground color (optional; current drawing color by default)
wDrawing width (nothing drawn below the minimum width)
x, yDrawing position

Returns: Original QR code pixel width [21, 177].

Note: Only active pixels (black modules) are drawn; draw the background beforehand. Nothing is drawn if the image extends beyond the screen.

cls(white)
local w = qrcode('https://example.com', black, 120, 60, 60)

10. Complete API Index​

Canvas Operations​

FunctionDescription
cls([col])Clear screen (and reset clipping region and print state)
camera([x, y])Set/reset screen offset → previous offset
clip([x, y, w, h [, clip_previous]])Set/reset clipping region → previous clipping region
flip()Present the current frame immediately

Colors​

FunctionDescription
color([col])Set current drawing color → previous color
palrgb(idx, r, g, b, a)Set palette color → color value
getcolor(r, g, b [, a])RGB → color value
pal(c0, c1 [, p])Color remapping (p: 0 drawing / 1 display) → previous mapping
palt(c [, t])Color transparency → previous transparency bitfield
fillp(p)Fill pattern → previous pattern

Pixels​

FunctionDescription
pset(x, y [, col])Set a pixel
pget(x, y)Get a pixel color
sget(x, y)Get a sprite sheet pixel
sset(x, y [, col])Set a sprite sheet pixel

Shapes​

FunctionDescription
line(x0, y0 [, x1, y1 [, col]])Draw a line
lines(coords [, col [, close]])Polyline
wline(width, x0, y0, x1, y1 [, col])Line with width
bezier(coords [, segments [, col [, width]]])Bezier curve
rect(x0, y0, x1, y1 [, col])Rectangle outline
rectfill(x0, y0, x1, y1 [, col])Filled rectangle
rectcls(x0, y0, x1, y1, col)Filled rectangle (bypasses pal/fillp)
circ(x, y, r [, col])Circle outline
circfill(x, y, r [, col])Filled circle
oval(x0, y0, x1, y1 [, col])Ellipse outline
ovalfill(x0, y0, x1, y1 [, col])Filled ellipse
arc(x, y, r, a1, a2 [, col])Arc
polygonfill(coords [, col])Filled polygon
trifill(x0, y0, x1, y1, x2, y2 [, col])Filled triangle

Sprites​

FunctionDescription
spr(n, x, y [, w, h [, fx, fy]])Draw a sprite
sspr(sx, sy, sw, sh, dx, dy [, dw, dh [, fx, fy]])Stretch a sprite
rotspr(sx, sy, sw, sh, dx, dy, angle, zx, zy)Rotate and scale a sprite
tspr(x0, y0, x1, y1, sx, sy [, sdx, sdy])Sprite-textured line
fget(n [, f])Get sprite flags
fset(n [, f], val)Set sprite flags

Other​

FunctionDescription
blit(x, y, opflag, data, ...)Image block operation → boolean
qrcode(str [, col [, w [, x, y]]])Draw a QR code → original width