Skip to content

Commit

Permalink
bug fix for the broken LAB / HSL mode in clustering
Browse files Browse the repository at this point in the history
  • Loading branch information
drake7707 committed Sep 11, 2019
1 parent 149ab21 commit 89ecb3f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ define("colorreductionmanagement", ["require", "exports", "common", "lib/cluster
// determine the weight (#pointsOfColor / #totalpoints) of each color
const weight = pointsByColor[color].length / (imgData.width * imgData.height);
const vec = new clustering_1.Vector(data, weight);
vec.tag = rgb;
vectors[vIdx++] = vec;
}
const random = new random_1.Random(settings.randomSeed);
Expand Down Expand Up @@ -493,6 +494,8 @@ define("colorreductionmanagement", ["require", "exports", "common", "lib/cluster
else {
rgb = centroid.values;
}
// remove decimals
rgb = rgb.map(v => Math.floor(v));
if (restrictToSpecifiedColors) {
if (settings.kMeansColorRestrictions.length > 0) {
// there are color restrictions, for each centroid find the color from the color restrictions that's the closest
Expand Down Expand Up @@ -527,8 +530,9 @@ define("colorreductionmanagement", ["require", "exports", "common", "lib/cluster
}
}
}
let pointRGB = v.tag;
// replace all pixels of the old color by the new centroid color
const pointColor = `${v.values[0]},${v.values[1]},${v.values[2]}`;
const pointColor = `${Math.floor(pointRGB[0])},${Math.floor(pointRGB[1])},${Math.floor(pointRGB[2])}`;
for (const pt of pointsByColor[pointColor]) {
const ptx = pt % imgData.width;
const pty = Math.floor(pt / imgData.width);
Expand Down
8 changes: 7 additions & 1 deletion src/colorreductionmanagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class ColorReducer {
const weight = pointsByColor[color].length / (imgData.width * imgData.height);

const vec = new Vector(data, weight);
vec.tag = rgb;
vectors[vIdx++] = vec;
}

Expand Down Expand Up @@ -170,6 +171,9 @@ export class ColorReducer {
rgb = centroid.values;
}

// remove decimals
rgb = rgb.map(v => Math.floor(v));

if (restrictToSpecifiedColors) {
if (settings.kMeansColorRestrictions.length > 0) {
// there are color restrictions, for each centroid find the color from the color restrictions that's the closest
Expand Down Expand Up @@ -205,8 +209,10 @@ export class ColorReducer {
}
}

let pointRGB: number[] = v.tag;

// replace all pixels of the old color by the new centroid color
const pointColor = `${v.values[0]},${v.values[1]},${v.values[2]}`;
const pointColor = `${Math.floor(pointRGB[0])},${Math.floor(pointRGB[1])},${Math.floor(pointRGB[2])}`;
for (const pt of pointsByColor[pointColor]) {
const ptx = pt % imgData.width;
const pty = Math.floor(pt / imgData.width);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/clustering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Random } from "../random";

export class Vector {

public tag:any;

constructor(public values: number[], public weight: number = 1) { }

public distanceTo(p: Vector): number {
Expand Down

0 comments on commit 89ecb3f

Please sign in to comment.