0

I have 22 colors which I want to use to draw any image with just these 22 colors.

Image image = GetImageFromURL(""); // returns an image object
Bitmap bitmap = new Bitmap(image);

String[] colors = {
    "#FFFFFF", // white
    "#C1C1C1", // light grey
    "#EF130B", // red
    "#FF7100", // orange
    "#FFE400", // yellow
    "#00CC00", // light green
    "#00B2FF", // cyan
    "#231FD3", // blue
    "#A300BA", // purple
    "#D37CAA", // pink
    "#A0522D", // light brown
    "#000000", // black
    "#4C4C4C", // dark grey
    "#740B07", // dark red
    "#C23800", // dark orange
    "#E8A200", // dark yellow
    "#005510", // dark green
    "#00569E", // dark cyan
    "#0E0865", // dark blue
    "#550069", // dark purple
    "#A75574", // dark pink
    "#63300D", // dark brown
};

How would I do this best? I currently have no idea how to translate billions of colors to just these 22 colors...

I would appreciate any help, thanks in advance.

alansiqueira27
  • 8,129
  • 15
  • 67
  • 111
mtnair
  • 120
  • 5
  • 1
    Try finding closes match in your pallet with the color you want to convert to? Maybe calculate score based on all three different RGB. – Karolis Kajenas Feb 24 '18 at 22:25
  • 1
    About what @Karolis said, you can use vector math to find the distance from the pixel color to the palette colors and use the nearest one. – Gusman Feb 24 '18 at 22:31
  • One approach is to convert the image into 8-bits image using some image processing library or a third party tool https://nl.mathworks.com/help/matlab/ref/rgb2ind.html that would limit the number of available colors to 256. Then find the closest match. – Anton Sizikov Feb 24 '18 at 22:32
  • https://stackoverflow.com/questions/21856184/the-best-way-to-reduce-quantity-of-colors-in-bitmap-palette – Anton Sizikov Feb 24 '18 at 22:34
  • @AntonSizikov That one is about choosing a palette. This case already _has_ a palette. – Nyerguds Mar 01 '18 at 22:29
  • Why are your colours _strings_, though? [Convert them](https://stackoverflow.com/a/2109938/395685) to an array of `Color` objects. – Nyerguds Mar 01 '18 at 22:40
  • You should add more details. How is the application going to interact with the user? – alansiqueira27 Mar 01 '18 at 22:55
  • We already have this question: https://stackoverflow.com/questions/34557480/quantization-reduction-of-colors-of-image – Hans Passant Mar 01 '18 at 23:32
  • @HansPassant quantization is _creating_ a palette. That isn't applicable here; the palette is already available. (Also, that particular question uses `GetPixel` and `SetPixel`, and so does the answer. Ew.) – Nyerguds Mar 01 '18 at 23:48

1 Answers1

0

The closest colour match for each pixel can be found using Pythagorean distance in 3D colour space. I explained the full process here, including how to apply this change to an image in an efficient way:

How to convert a colored image to a image that has only two predefined colors?

This question was about a 2-colour palette, but as long as an 8-bit-per-pixel image is used as result, the process is exactly the same for any palette.

Nyerguds
  • 5,360
  • 1
  • 31
  • 63