I created a function that takes an image and returns a similar version of the image but with Gameboy graphics. Is there a way I can sort of save the function so I can use it in other projects similar to the way we use functions like circle(x, y, d)?
Here’s the function in case you need it:
PImage gameboyRenderer(PImage real){
PImage image = createImage(real.width, real.height, RGB);
image.copy(real, 0, 0, real.width, real.height, 0, 0, real.width, real.height);
for(int y = 0; y < image.height - 1; y++){
for(int x = 1; x < image.width - 1; x++){
color pix = image.pixels[index(x, y)];
float scale = 3;
float oldR = red(pix);
float oldG = green(pix);
float oldB = blue(pix);
float newR = (255 / scale) * round(scale * oldR / 255.0);
float newG = (255 / scale) * round(scale * oldG / 255.0);
float newB = (255 / scale) * round(scale * oldB / 255.0);
image.pixels[index(x, y)] = color(newR, newG, newB);
float errR = oldR - newR;
float errG = oldG - newG;
float errB = oldB - newB;
int index = index(x + 1, y );
color c = image.pixels[index];
float r = red(c);
float g = green(c);
float b = blue(c);
r = r + errR * 7 / 16.0;
g = g + errG * 7 / 16.0;
b = b + errB * 7 / 16.0;
image.pixels[index] = color(r, g, b);
index = index(x - 1, y + 1);
c = image.pixels[index];
r = red(c);
g = green(c);
b = blue(c);
r = r + errR * 3 / 16.0;
g = g + errG * 3 / 16.0;
b = b + errB * 3 / 16.0;
image.pixels[index] = color(r, g, b);
index = index(x , y + 1);
c = image.pixels[index];
r = red(c);
g = green(c);
b = blue(c);
r = r + errR * 5 / 16.0;
g = g + errG * 5 / 16.0;
b = b + errB * 5 / 16.0;
image.pixels[index] = color(r, g, b);
index = index(x + 1, y + 1);
c = image.pixels[index];
r = red(c);
g = green(c);
b = blue(c);
r = r + errR * 1 / 16.0;
g = g + errG * 1 / 16.0;
b = b + errB * 1 / 16.0;
image.pixels[index] = color(r, g, b);
}
}