# Generate a Pallette for an Image [duplicate]

This is a successor to my previous Code Golf challenge, which may be found here.

This time, you are trying to determine what the contents of colors.txt should be.

Your task is to take from the input the path to an image and a number of colours, and write to colors.txt a list of that many colors chosen for that image.

Remember, you are generating pallettes to go with this challenge.

As usual, you are going for lowest score, but in this case, your score is given by ceil(codeSize/(1+max(0,votes))), so upvotes will help you as well.

For one or more of the following images, you should test your generated pallettes (using one of the answers from this challenge to test them) and post the results for a variety of pallette sizes:

## marked as duplicate by Alex A., es1024, Community♦May 19 '15 at 1:45

• Apart from lifting the restriction that on the number of contiguous cells in the result, this seems to be asking for the same as Paint by Numbers. – Martin Ender May 18 '15 at 23:09
• So, do you think this should be deleted in that case? – SuperJedi224 May 19 '15 at 0:26

# Java

796 bytes

import java.awt.Color;import java.awt.image.*;import java.io.*;import java.util.*;import javax.imageio.*;public class P{public static void main(String[] a) throws Exception{Random R=new Random();Scanner in=new Scanner(System.in);PrintStream o=new PrintStream("colors.txt");List<Color> col=new ArrayList<Color>();BufferedImage i=ImageIO.read(new File(in.nextLine()));int h=i.getHeight(),w=i.getWidth(),k=in.nextInt(),x,y,z=0,r,g,b;while(col.size()<k&&z<k*k){x=R.nextInt(w);y=R.nextInt(h);Color c=new Color(i.getRGB(x, y));r=5*(c.getRed()/5);g=5*(c.getGreen()/5);b=5*(c.getBlue()/5);c=new Color(r,g,b);if(!col.contains(c)){col.add(c);}z++;}while(col.size()<k)col.add(new Color(R.nextInt(256),+R.nextInt(256),R.nextInt(256)));for(Color c:col)o.println(c.getRed()+" "+c.getGreen()+" "+c.getBlue());}}


Picks random colors from the image, with each component rounded to the nearest multiple of 5, such that no color is selected twice. If, after n2 selections, it has not yet found n distinct colors, it picks the rest entirely at random.

Some sample results:

The black spaceship in 10 colors:

In 25 colors:

In 50 colors:

In 200 colors:

The gold spaceship in 10 colors:

In 25 colors:

In 50 colors:

In 200 colors:

And, finally, the exploding globe in 10 colors:

In 25 colors:

In 50 colors:

And in 200 colors: