hi i've been playing around with colour sorting.
i found the example on the site here, not to be that accurate http://processing.org/learning/libraries/colorsorting.html
due to the fact that is used the sum of all the colours ad the basis for its formula: bright[count] = r*r + g*g + b*b;
so i used real brightness. then added two more checks into the loop to sort the hue and saturations. but it still doesn't seem 100% consistent visually. i know it is ideal to do hue first, but then a darker colour would show up in the middle of the palette.
here's an example of the output:
http://radarboy.com/george/palette.jpgi know i'm probably being anal here. if you look at the bottom there's B223 on B235 which visually seem like they should belong further up the palette. but their HSB values say otherwise.
anyway, it's not too serious. just mildly puzzling. and here's at least a class for anyone who wants to extract and sort colours....
Code:
int boxSize=40;
int numberColours=128;
SampleColour colors;
PFont font;
void setup(){
size (800,600);
stroke(255);
//noStroke();
font = loadFont("Helvetica-9.vlw");
textFont(font, 9);
colors = new SampleColour(numberColours, "v.gif");
strokeWeight(4);
showColours();
//smooth();
}
void draw(){
}
void keyPressed(){
showColours();
}
void showColours(){
println("showcolours");
background(255);
int xpos=0;
int ypos=0;
for (int i=0; i< numberColours; i++) {
if (xpos >= width) {
xpos =0;
ypos+=boxSize+40;
}
color c = colors.colourNumber[i];
fill(c);
rect(xpos,ypos,boxSize,boxSize);
fill(100);
text("B:"+int(colors.bright[i]), xpos+2, ypos+boxSize+8);
text("H:"+int(colors.hues[i]), xpos+2, ypos+boxSize+20);
text("S:"+int(colors.saturations[i]), xpos+2, ypos+boxSize+32);
xpos+=boxSize;
}
}
//============== colour sampling
// sampling adapted http://abandonedart.org/
// color sort adapted from ben fry http://processing.org/
class SampleColour {
int numberColours;
color[] colourNumber;
PImage img;
// int[] bright;
float[] bright;
float[] hues;
float[] saturations;
SampleColour(int _numberColours, String _img) {
numberColours = _numberColours;
colourNumber = new color[numberColours];
//bright = new int[numberColours];
bright = new float[numberColours];
hues = new float[numberColours];
saturations = new float[numberColours];
img = loadImage(_img);
sampleColour();
}
void sampleColour() {
image(img,0,0);
int count = 0;
for (int x=0; x < img.width; x+=2){
for (int y=0; y < img.height; y+=2) {
color c = get(x,y);
if (checkDuplicates(c, count)) {
// println("Duplicate!");
continue;
}
int r = c >> 16 & 0xff;
int g = c >> 8 & 0xff;
int b = c & 0xff;
println(r);
if (count < numberColours) {
colourNumber[count]= c;
hues[count]= hue(color(r,g,b));
saturations[count]= saturation(color(r,g,b));
// bright[count] = r*r + g*g + b*b;
bright[count] = brightness(color(r,g,b));
//bright[count] = brightness(color(r,g,b))+hues[count];
//bright[count] = brightness(color(r,g,b))+hues[count]+saturations[count];
count++;
}
}
}
sortColours(numberColours, bright, colourNumber);
//sortColours(numberColours, hues, colourNumber);
//
sortHS(saturations,hues);
sortHS(hues,bright);
//
background(255);
println(count);
println(colourNumber.length);
}
// Functions to handle sorting the color data
void sortColours(int length, float[] a, color[] stuff) {
sortSub(a, stuff, 0, length - 1);
}
void sortSwap(float[] a, color[] stuff, int i, int j) {
//int T = a[i];
float T = a[i];
a[i] = a[j];
a[j] = T;
color v = stuff[i];
stuff[i] = stuff[j];
stuff[j] = v;
}
void sortSub(float[] a, color[] stuff, int lo0, int hi0) {
int lo = lo0;
int hi = hi0;
//int mid;
float mid;
if (hi0 > lo0) {
mid = a[(lo0 + hi0) / 2];
while (lo <= hi) {
while ((lo < hi0) && (a[lo] < mid)) {
++lo;
}
while ((hi > lo0) && (a[hi] > mid)) {
--hi;
}
if (lo <= hi) {
sortSwap(a, stuff, lo, hi);
++lo;
--hi;
}
}
if (lo0 < hi)
sortSub(a, stuff, lo0, hi);
if (lo < hi0)
sortSub(a, stuff, lo, hi0);
}
}
void sortHS(float[] sortable, float[] compare){
int hCount = 0;
int startCount = 0;
for (int i=1; i< numberColours; i++){
if (compare[i-1]==compare[i]){
hCount++;
} else {
// fire this once they're differnce, so we're now looking actually at i-1
if (hCount>0){
startCount = i-1-hCount;
sortSub(sortable, colourNumber, startCount, i - 1);
hCount = 0;
}
}
}
}
boolean checkDuplicates(color _c, int _count){
int duplicates = 0;
for (int i=0; i < _count; i++) {
if (colourNumber[i] == _c) {
duplicates++;
}
}
//println ("Duplicates: "+ duplicates);
if (duplicates>0) {
return true;
}
else {
return false;
}
}
}