pebble-dither

1.0.3 • Public • Published

Dither Library for Pebble

Draw 6bit or 1bit dithered rectangles or replace a color in a screen region with a dithered color.

For the source and an example program, see the github page for this library:
https://github.com/robisodd/dither-library/blob/master/src/main.c
or if you are using CloudPebble, import it by:
https://cloudpebble.net/ide/import/github/robisodd/dither-library

TL;DR How to use:

Include:
#include <pebble-dither/dither.h>
Draw:
fill_rect_dithered(ctx, GRect(10, 10, 50, 50), 0, 255, 96);
Replace Within Region:
graphics_context_set_fill_color(ctx, GColorCeleste);
graphics_fill_circle(ctx, GPoint(80, 100), 20);
graphics_fill_circle(ctx, GPoint(20, 120), 10);
replace_color_in_rect_with_dithered(ctx, GRect(60, 80, 40, 40), GColorCeleste, 52, 192, 12);
Replace Screen Color:
graphics_context_set_fill_color(ctx, GColorRed);
graphics_fill_circle(ctx, GPoint(rand() % 100, rand() % 100), 10);
replace_color_with_dithered(ctx, GColorRed, 52, 192, 12);
TL;DR Notes:
  • The coordinates for rect are in absolute screen coordinates and will not move with the layer they are called in.
  • Also, rect will not be clipped by any layers.
  • To reduce CPU time, use replace_color_in_rect_with_dithered() instead of replace_color_with_dithered() whenever possible.
  • Replace functions only replaces one color per call and therefore does not work on anti-aliased shapes.
  • Animated shapes won't flicker when drawn in different locations.

What it does

24bit color to 6bit color or 1bit black-and-white dithering library for Pebble. Tested on Aplite, Basalt and Chalk. Should be forward compatible with Diorite, Emery and future watches.

Draws a filled rectangle to the screen which is dithered from 24bit (3 channels of 8bit colors: red, green and blue) color down to Pebble's native 6bit color, or even further down to 1bit black-and-white.

Also can replace screen colors with the dithered color, allowing you to use native pebble API calls (e.g. graphics_draw_line, graphics_fill_circle, gpath_draw_filled, etc.) with dithered colors. To do this, draw with the Pebble function using a placeholder color that doesn't occur anywhere else on the screen, then call replace_color_with_dithered (see: below) to replace that placeholder color with the dithered color. Or use replace_color_in_rect_with_dithered to only replace colors within a screen region.

Currently it does not work on antialiased shapes, unless you specifically replace each color. For instance, if you draw an antialiased red (GColorRed=0b11110000) line on a black background, you'd have to run the replacement funciton 3 times: once for red, once for darker red (GColorDarkCandyAppleRed=0b1110000) and once for darkest red (GColorBulgarianRose=0b11010000). Each will be replaced with the full dithered color, so the input 24bit RGB must be darkened to reproduce the antialiased look.

Screenshots

Aplite:

Basalt:

Chalk:


Function calls

fill_rect_dithered(ctx, rect, r, g, b)

Given a graphics context, a rect (absolute screen coordinates), and a 24bit RGB color, draws a rectangle filled with a dithered (patterned) 6-bit color. Note that rect uses absolute screen coordinates, unlike graphics_fill_rect() which draws relative to its layer.

replace_color_with_dithered(ctx, replacement_color, r, g, b)

Given a graphcs context, a color to look for, and a 24bit RGB dithered color to replace it with, searches the ENTIRE SCREEN for replacement_color and replaces it with a 6bit color dithered from the 24bit RGB given. Note that it currently doesn't ignore the alpha channel, though screen pixels are nearly always alpha = 3.

replace_color_in_rect_with_dithered(ctx, rect, replacement_color, r, g, b)

Same as replace_color_with_dithered() but only searches within the rect for replacement_color. Note that rect uses absolute screen coordinates, unlike normal Pebble API functions (e.g. graphics_fill_rect()) which draw relative to their layer.

Package Sidebar

Install

npm i pebble-dither

Weekly Downloads

3

Version

1.0.3

License

MIT

Last publish

Collaborators

  • robisodd