Sign in to follow this  

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.

Share this post

Link to post
Share on other sites

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 I think setting each Posterize value to 8 (for Red, Green, Blue) will result in 256 possible colors

Share this post

Link to post
Share on other sites

jxp, the original request predates the postertize plugin, but thank you for your helpful response.

SweterdJ, this is a classic example of why necroposting is against the rules...

Share this post

Link to post
Share on other sites
This topic is now closed to further replies.
Sign in to follow this