Skip to content

Commit beeea4c

Browse files
committed
dialog claer: speed up color sampling; fill with more accurate background color
1 parent 5374870 commit beeea4c

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

src/dialog_clear.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,62 @@
22

33
namespace LabelPlus {
44

5+
// note: too slow
6+
// function getColor(doc: Document, x: UnitValue, y: UnitValue): SolidColor
7+
// {
8+
// let sample = doc.colorSamplers.add([x, y]);
9+
// let color = sample.color;
10+
// sample.remove();
11+
// return color;
12+
// }
13+
514
function getColor(doc: Document, x: UnitValue, y: UnitValue): SolidColor
615
{
7-
let sample = doc.colorSamplers.add([x, y]);
8-
let color = sample.color;
9-
sample.remove();
16+
let x_px = Math.floor(x.as("px"));
17+
let y_px = Math.floor(y.as("px"));
18+
19+
var st = doc.activeHistoryState;
20+
21+
Stdlib.selectBounds(doc, [x_px, y_px, x_px+1, y_px+1]);
22+
23+
let color = getSelectionColor(doc);
24+
25+
doc.activeHistoryState = st;
26+
1027
return color;
1128
}
1229

30+
function getSelectionColor(doc: Document): SolidColor
31+
{
32+
function findPV(h: number[]) {
33+
let max = 0;
34+
for (var i = 0; i <= 255; i++) {
35+
if (h[i] > h[max]) {
36+
max = i;
37+
}
38+
}
39+
return max;
40+
}
41+
42+
let pColour = new SolidColor();
43+
44+
if (doc.mode == DocumentMode.RGB) {
45+
pColour.model = ColorModel.RGB;
46+
pColour.rgb.red = findPV(doc.channels[0].histogram);
47+
pColour.rgb.green = findPV(doc.channels[1].histogram);
48+
pColour.rgb.blue = findPV(doc.channels[2].histogram);
49+
}
50+
else if (doc.mode == DocumentMode.GRAYSCALE) {
51+
let gr = findPV(doc.channels.getByName("Gray").histogram);
52+
pColour.model = ColorModel.GRAYSCALE;
53+
pColour.gray.gray = 100 * (gr / 255);
54+
}
55+
else {
56+
log("getSelectionColor: Color Mode not supported: " + doc.mode);
57+
}
58+
return pColour;
59+
}
60+
1361
function isSelectionValid()
1462
{
1563
try {
@@ -53,14 +101,14 @@ export function dialogClear(doc: Document, bgLayer: ArtLayer, overLayer: ArtLaye
53101
for (let i = 0; i < labels.length; i++) {
54102
let x = labels[i].x * width;
55103
let y = labels[i].y * height;
56-
let fill_color = getColor(doc, UnitValue(x, 'px'), UnitValue(y, 'px'));
57-
58-
log("point " + i + "(" + x + "," + y + ") color=" + fill_color.rgb.hexValue.toString());
59104

60105
app.activeDocument.activeLayer = bgLayer;
61106
doc.selection.deselect();
62107
MyAction.magicWand(x, y, tolerance, false, true, 'addTo');
63108

109+
let fill_color = getSelectionColor(doc);
110+
log("point " + i + "(" + x + "," + y + ") color=" + fill_color.rgb.hexValue.toString());
111+
64112
let tmp_layer = doc.artLayers.add();
65113
app.activeDocument.activeLayer = tmp_layer;
66114
doc.selection.fill(tmp_color, ColorBlendMode.NORMAL, 100, false);

0 commit comments

Comments
 (0)