# My projects library #4! Gallery Megathread

**URL:** https://discourse.processing.org/t/my-projects-library-4-gallery-megathread/31103
**Category:** Gallery
**Created:** [July 7, 2021, 11:46am UTC](https://discourse.processing.org/t/my-projects-library-4-gallery-megathread/31103 "2021-07-07T11:46:48Z")
**Posts on this page:** 1
**Showing post:** 20

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [August 30, 2021, 3:30pm UTC](https://discourse.processing.org/t/my-projects-library-4-gallery-megathread/31103/20 "2021-08-30T15:30:00Z")

</div>

**#55 Simple posterization filter**

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b08cc4d3793af09f3213d98df982bd77ab02e812.jpeg)

Posterization filter sets the maximum amount of colors used. All colors on the screen get converted into those. [explanation by josephh](https://discourse.processing.org/t/posterization-filter-explained/32001/3)

> **Code**
>
> ```auto
> int clrs = 3; //end has clrs^3 colors, (1-1, 2-8, 3-27, 4-64, 5-125, 6-216,...)
> color clr[];
> PImage img;
> void setup() {
> size(640,642);
> clr = generateColors(clrs);
> img = loadImage("house2.jpg");
> println(img.width,img.height);
> }
> void draw() {
> image(img,0,0); noLoop();
> loadPixels();
> for(int i = 0; i < width; i++) for(int j = 0; j < height*0.5; j++) {
> int in = i+j*width, off = floor(width*height*0.5);
> color base = pixels[in], chan = closest(base);
> pixels[in+off] = chan;
> }
> updatePixels();
> }
> color closest(color input) {
> int id=0;
> float d = -1;
> for(int i = 0; i < clr.length; i++) {
> float dis = dist(red(input),green(input),blue(input),red(clr[i]),green(clr[i]),blue(clr[i]));
> if(dis < d || d == -1) {
> id = i;
> d = dis;
> }
> }
> return clr[id];
> }
> 
> color[] generateColors(int c) {
> float a = 255/c;
> color nclr[] = new color[c*c*c];
> for(int i = 0; i < c; i++) for(int j = 0; j < c; j++) for(int k = 0; k < c; k++) {
> nclr[i+j*c+k*c*c] = color(i*a, j*a, k*a);
> }
> return nclr;
> }
> 
> ```

It is a simple project (if you are using the simple color cube solution). It can be upgraded at any time by changing the generateColors() function.

---

_[View the full topic](https://discourse.processing.org/t/my-projects-library-4-gallery-megathread/31103)._
