#target photoshop
preferences.rulerUnits = Units.PIXELS;
function cTID(s) { return app.charIDToTypeID(s); }
function sTID(s) { return app.stringIDToTypeID(s); }
function RGBc(r, g, b) {
var color = new ActionDescriptor();
color.putDouble(cTID("Rd "), r);
color.putDouble(cTID("Grn "), g);
color.putDouble(cTID("Bl "), b);
return color;
}
function selectColorRange(color1, color2) {
var desc = new ActionDescriptor();
desc.putInteger(cTID("Fzns"), 0);
desc.putObject(cTID("Mnm "), cTID("RGBC"), color1);
desc.putObject(cTID("Mxm "), cTID("RGBC"), color2);
executeAction(cTID("ClrR"), desc, DialogModes.NO);
}
function selectRange(x, y, w, h) {
var dx = w + x;
var dy = h + y;
return [[x, y], [dx, y], [dx, dy], [x, dy]];
}
var util = {
log: function(str, mode) {
var f = new File("D:/backup/photoshop-script/test.log");
if (mode == undefined) { mode = "a"; }
f.open(mode);
f.write(str + "\r\n");
f.close();
f = null;
},
logPos: function(msg, x, y, mode) {
var s = msg + ": (" + x + "," + y + ")";
this.log(s, mode);
},
point1: function(selection) {
var b = selection.bounds;
var p1 = {
x: b[0].as("px"),
y: b[1].as("px"),
};
var p2 = {
x: b[2].as("px"),
y: b[3].as("px"),
};
var startPos = {
x: p2.x,
y: p2.y,
};
var sample = app.activeDocument.colorSamplers.add([startPos.x, startPos.y]);
var isFlag = false;
var findPos = { x: 0, y: 0 };
var nums = [];
for (var i = 0; i < p2.x - p1.x; ++i) {
var pos = { x: startPos.x - i, y: startPos.y };
sample.move([pos.x, pos.y]);
var hex = sample.color.rgb.hexValue;
if (hex != "FFFFFF") {
findPos.x = pos.x;
findPos.y = pos.y;
break;
}
}
sample = null;
return findPos;
},
point2: function(selection) {
var b = selection.bounds;
var p1 = {
x: b[0].as("px"),
y: b[1].as("px"),
};
var p2 = {
x: b[2].as("px"),
y: b[3].as("px"),
};
var startPos = {
x: p1.x,
y: p2.y,
};
var sample = app.activeDocument.colorSamplers.add([startPos.x, startPos.y]);
var isFlag = false;
var findPos = { x: 0, y: 0 };
var nums = [];
for (var i = 0; i < p2.y - p1.y; ++i) {
var pos = { x: startPos.x, y: startPos.y - i };
sample.move([pos.x, pos.y]);
var hex = sample.color.rgb.hexValue;
if (hex != "FFFFFF") {
findPos.x = pos.x;
findPos.y = pos.y;
break;
}
}
sample = null;
return findPos;
},
selectionRotate: function (pos1, pos2) {
var radian = Math.atan2(pos2.y - pos1.y, pos2.x - pos1.x);
var degree = (radian * 180.0) / Math.PI;
return degree;
},
t1: function() {
var dir = "D:/backup/photoshop-script/images/";
var name = "500x500.png";
var file = new File(dir + name);
var doc = app.open(file);
app.activeDocument.flatten();
selectColorRange(
RGBc(0.0, 0.0, 0.0),
RGBc(0.0, 0.0, 0.0)
);
var pos1 = this.point1(app.activeDocument.selection);
var pos2 = this.point2(app.activeDocument.selection);
this.logPos("pos1", pos1.x, pos1.y);
this.logPos("pos2", pos2.x, pos2.y);
var rot = this.selectionRotate(pos2, pos1);
app.activeDocument.selection.rotate(-rot);
selectColorRange(
RGBc(0.0, 0.0, 0.0),
RGBc(0.0, 0.0, 0.0)
);
var b = app.activeDocument.selection.bounds;
var re = {
x: b[0].as("px"),
y: b[1].as("px"),
dx: app.activeDocument.width.as("px"),
dy: app.activeDocument.height.as("px")
};
var range = selectRange(re.x, re.y, re.dx, re.dy);
app.activeDocument.selection.translate(-re.x, -re.y);
app.activeDocument.selection.deselect();
},
};
util.t1();