/** * Constants */ const byte BLACK = 0; const byte WHITE = 255; const byte DARK_GRAY = 73; const byte GRAY = 146; byte selected_color = 255; plex System { ref function vector; byte wst; byte rst; u32 pad; nat r; nat g; nat b; byte debug; byte halt; } plex Screen { ref function vector; nat width; nat height; nat auto; nat x; nat y; nat addr; byte pixel; byte sprite; } plex Mouse { ref function vector; nat x; nat y; nat state; nat z; nat scrollx; nat scrolly; nat unused_; } System system = mmap(0x00, System); Screen screen = mmap(0x20, Screen); screen.vector = ref render_screen; Mouse mouse = mmap(0x90, Mouse); mouse.vector = ref render_screen; halt; function on_mouse_down() { int box_size = 20; int x = 1; int y = 1; byte color = BLACK; outlined_swatch(screen, color, x, y); set_color(box_size, x, y, mouse.x, mouse.y, color); color = WHITE; x = 21; outlined_swatch(screen, color, x, y); set_color(box_size, x, y, mouse.x, mouse.y, color); rectangle(screen, selected_color, x, y, 5, 5); } function render_screen() { outline_swatch(screen, BLACK, 1, 1); outline_swatch(screen, WHITE, 21, 1); } /** * Checks if the click is within the bound and update the selected color if so. */ function set_color(int box_size, int bx, int by, int mx, int my, byte color) { int right = bx + box_size; int bottom = by + box_size; if (mx < bx) return; if (mx > right) return; if (my < by) return; if (my > bottom) return; selected_color = color; } /** * Draw a color box with a grey outline, if selected use a darker color */ function outline_swatch(byte color, int x, int y) { byte bg_color = GRAY; if (selected_color == color) { bg_color = DARK_GRAY; } rectangle(bg_color, x, y, 20, 20); rectangle(color, x + 2, y + 2, 17, 17); } /** * Draw a rectangle */ function rectangle(byte color, int x, int y, int width, int height) { int base = y * screen.width + x; for (int i = height; i > 0; i--) { int row = base + width; for (int j = width; j < row; j++) { screen.x = i; screen.y = j; screen.pixel = color; } base += screen.width; } }