The ns.diffSort function has a bug. The indicated code should be inside the loop above it. Otherwise, the loop above simply recomputes "d" several times and the only iteration has any subsequent effect.
ns.diffSort = function(colorsToSort, distanceType){
// Sort
var diffColors = [colorsToSort.shift()];
while(colorsToSort.length>0){
var index = -1;
var maxDistance = -1;
for(candidate_index=0; candidate_index<colorsToSort.length; candidate_index++){
var d = Infinity;
for(i=0; i<diffColors.length; i++){
var colorA = colorsToSort[candidate_index].lab();
var colorB = diffColors[i].lab();
var d = ns.getColorDistance(colorA, colorB, distanceType);
/* *** Code below belongs here *** */
}
// **** BEGIN
if(d > maxDistance){
maxDistance = d;
index = candidate_index;
}
//**** END
}
var color = colorsToSort[index];
diffColors.push(color);
colorsToSort = colorsToSort.filter(function(c,i){return i!=index;});
}
return diffColors;
}
The ns.diffSort function has a bug. The indicated code should be inside the loop above it. Otherwise, the loop above simply recomputes "d" several times and the only iteration has any subsequent effect.