Skip to content

Instantly share code, notes, and snippets.

@jacobjoaquin
Created March 19, 2014 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobjoaquin/9643008 to your computer and use it in GitHub Desktop.
Save jacobjoaquin/9643008 to your computer and use it in GitHub Desktop.
Processing sketch. Translates camera vision into a commodore.
/*
c64cam - Commodore 64 Vision with your Camera
Jacob Joaquin
jacobjoaquin@gmail.com
*/
import processing.video.*;
Capture cam;
int cam_width = 160;
int cam_height = 200;
int n_colors;
int[] palette = {
0, 0, 0,
255, 255, 255,
152, 75, 67,
121, 193, 200,
155, 81, 165,
104, 174, 92,
82, 66, 157,
201, 214, 132,
155, 103, 57,
106, 84, 0,
195, 123, 117,
99, 99, 99,
138, 138, 138,
163, 229, 153,
138, 123, 206,
173, 173, 173};
void setup() {
size(320, 200);
cam = new Capture(this, cam_width, cam_height);
n_colors = palette.length / 3;
}
void draw() {
if (cam.available() == true) {
cam.read();
cam.loadPixels();
for (int x = 0; x < cam_width; x++) {
for (int y = 0; y < cam_height; y++) {
int L = x + y * cam_width;
color c = cam.pixels[L];
float r = red(c);
float g = green(c);
float b = blue(c);
int x2 = x * 2;
closestPaletteColor((int) r, (int) g, (int) b);
line(x2, y, x2 + 1, y);
}
}
}
}
int colorProximity(int r, int g, int b, int r2, int g2, int b2) {
r -= r2;
g -= g2;
b -= b2;
return r * r + g * g + b * b;
}
void closestPaletteColor(int r, int g, int b) {
int closest = 0;
int min = 195075; // 255 * 255 * 3;
int proximity;
for (int i = 0; i < n_colors; i++) {
int j = i * 3;
proximity = colorProximity(r, g, b, palette[j], palette[j + 1], palette[j + 2]);
if (proximity < min) {
min = proximity;
closest = j;
}
}
stroke(palette[closest], palette[closest + 1], palette[closest + 2]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment