|
2 | 2 |
|
3 | 3 | namespace LabelPlus { |
4 | 4 |
|
| 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 | + |
5 | 14 | function getColor(doc: Document, x: UnitValue, y: UnitValue): SolidColor |
6 | 15 | { |
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 | + |
10 | 27 | return color; |
11 | 28 | } |
12 | 29 |
|
| 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 | + |
13 | 61 | function isSelectionValid() |
14 | 62 | { |
15 | 63 | try { |
@@ -53,14 +101,14 @@ export function dialogClear(doc: Document, bgLayer: ArtLayer, overLayer: ArtLaye |
53 | 101 | for (let i = 0; i < labels.length; i++) { |
54 | 102 | let x = labels[i].x * width; |
55 | 103 | 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()); |
59 | 104 |
|
60 | 105 | app.activeDocument.activeLayer = bgLayer; |
61 | 106 | doc.selection.deselect(); |
62 | 107 | MyAction.magicWand(x, y, tolerance, false, true, 'addTo'); |
63 | 108 |
|
| 109 | + let fill_color = getSelectionColor(doc); |
| 110 | + log("point " + i + "(" + x + "," + y + ") color=" + fill_color.rgb.hexValue.toString()); |
| 111 | + |
64 | 112 | let tmp_layer = doc.artLayers.add(); |
65 | 113 | app.activeDocument.activeLayer = tmp_layer; |
66 | 114 | doc.selection.fill(tmp_color, ColorBlendMode.NORMAL, 100, false); |
|
0 commit comments