Jump to content

Plug-in Request - convert to 256 colors

Recommended Posts

int interval = 64;
int R;
int B;
int G;
int A;
float divR;
float divB;
float divG;
float divA;
ColorBgra fin;

void Render(Surface dst, Surface src, Rectangle rect)
   for(int y = rect.Top; y < rect.Bottom; y++)

       for (int x = rect.Left; x < rect.Right; x++)
          ColorBgra source = src[x, y];
           divR = source.R/interval;
           divB = source.B/interval;
           divG = source.G/interval;
           divA = source.A/interval;
           R = (int)System.Math.Round(divR);
           B = (int)System.Math.Round(divB);
           G = (int)System.Math.Round(divG);
           A = (int)System.Math.Round(divA);
           R = R * interval;
           B = B * interval;
           G = G * interval;
           A = 255; //A * interval;
           fin.R = (byte)(R);
           fin.B = (byte)(;
           fin.G = (byte)(G);
           fin.A = (byte)(A);
           dst[x, y] =  fin;

I love math.

Explanation: a codelab script to make 256 colors without diffusion or dithering.

Also, for 16 colors, change int interval = 64; to int interval = 128; . And yes, this can be simplified. I just don't feel like it.

Link to post
Share on other sites
  • 3 years later...

I have a low color file type plugin that supports saving images to 1bit, 4bit, 8bit and 16bit color depths. viewtopic.php?f=16&t=30638

Also without examining the code posted too much, does it do anything different to the Posterize effect (incuded in Paint.net)? I think setting each Posterize value to 8 (for Red, Green, Blue) will result in 256 possible colors

Link to post
Share on other sites


This topic is now archived and is closed to further replies.

This topic is now closed to further replies.
  • Create New...