Skip to content

Commit

Permalink
new algo based on hsl
Browse files Browse the repository at this point in the history
  • Loading branch information
kamalkishor1991 committed Sep 23, 2014
1 parent 1cca905 commit 1009f02
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
Binary file modified croma.jar
Binary file not shown.
67 changes: 64 additions & 3 deletions src/me/croma/image/KMeansColorPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,71 @@
* Using KMeans algorithm for clustering colors.
*/
public class KMeansColorPicker implements ColorPicker {
final ColorSpaceConverter cs = new ColorSpaceConverter();
public KMeansColorPicker() {
//default constructor
}
@Override
public List<Color> getUsefulColors(Image img, int noOfColors) throws IOException {

@Override
public List<Color> getUsefulColors(Image image, int noOfColors) throws IOException {
int m = 4;
List<Color> l = getKColors(image, noOfColors * m);
Collections.sort(l, new Comparator<Color>() {
@Override
public int compare(Color o1, Color o2) {
float d1[] = o1.getHSB();
float d2[] = o2.getHSB();
return d1[0] >= d2[0] ? 1 : -1;
}
});
int n = m/2;
List<Color> r = new ArrayList<Color>();
for (int i = 0;i < l.size();i += m) {
List<Color> ll = new ArrayList<Color>();
for (int j = 0;j < m;j++) {
if (i + j < l.size()) ll.add(l.get(i + j));
}
Collections.sort(ll, new Comparator<Color>() {
@Override
public int compare(Color o1, Color o2) {
return Double.compare(o1.getHSB()[1], o2.getHSB()[1]);
}
});
for (int j = 0;j < n;j++) {
if (ll.size() - j > 0) r.add(ll.get(ll.size() - 1 - j));//add pure colors.
}
//r.add(ll.get(ll.size() - 1));
}

List<Color> rr = new ArrayList<Color>();
for (int i = 0;i < r.size();i += n) {
List<Color> ll = new ArrayList<Color>();
for (int j = 0;j < n;j++) {
if (i + j < r.size()) ll.add(r.get(i + j));
}
Collections.sort(ll, new Comparator<Color>() {
@Override
public int compare(Color o1, Color o2) {
return Double.compare(o1.getHSB()[2], o2.getHSB()[2]);
}
});
rr.add(ll.get(ll.size() - 1));
//r.add(ll.get(ll.size() - 1));
}





//List<Color> r = l.subList(0, noOfColors);
for (int i = 0 ;i < l.size();i++) {
System.out.print(l.get(i).getHSB()[0] + ",");
}
return rr;
}


public List<Color> getKColors(Image img, int noOfColors) throws IOException {
int mx = Math.max(img.getHeight(), img.getWidth());
int mn = Math.min(img.getHeight(), img.getWidth());
int sh = Math.min(100, mx);
Expand All @@ -24,7 +84,7 @@ public List<Color> getUsefulColors(Image img, int noOfColors) throws IOException
int h = image.getHeight();
int w = image.getWidth();
double input[][] = new double[h*w][3];
ColorSpaceConverter cs = new ColorSpaceConverter();

int index = 0;
for (int i = 0;i < w;i++) {
for (int j = 0;j < h;j++) {
Expand Down Expand Up @@ -67,4 +127,5 @@ private double distance(double input1[], double input2[]) {
}



}
7 changes: 6 additions & 1 deletion src/me/croma/test/TestColorPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private static void genHtml(File file, int algo, int noC, PrintStream ps) throws
String s = "<div color=\"#3CA\" style=\"\n" +
" size: a3;\n" +
" width: 50px;\n" +
"display: table-cell;\n" +
" height: 50px;margin:10px;\n" +
" background: %s\n" +
"\"></div>";
Expand All @@ -73,10 +74,14 @@ private static void genHtml(File file, int algo, int noC, PrintStream ps) throws

ps.println("<IMG src = './" + file.getName() + "' style=\"\n" +
" width: 500;\n" +
"\"></IMG>");
"\"></IMG><div style=\"\n" +
" margin-top: 10px;\n" +
" margin-bottom: 10px;\n" +
"\"><p>" + file.getName() + "</p>");
for (Color c : l) {
ps.print(String.format(s, "#" + String.format("%06x", c.getRGB() & 0x00FFFFFF)) + " ");
}
ps.print("</div>");
}


Expand Down
Binary file added test/2014 - 1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1009f02

Please sign in to comment.