While converting a percent UnitValue to pixels I ran into a little problem with the as('px') conversion.
Is this a known bug in CS2? I looked around and didn't find anything about it, but it is past 3:00AM so...
Code: Select allvar origRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PERCENT;
var pctVal = UnitValue(50, "%");
var baseVal = UnitValue(10, "in");
pctVal.baseUnit = baseVal;
var inchVal = pctVal.as("in");
var pixVal = pctVal.as("px"); // <--- result is NaN on CS2
var msg = "Percent UnitValue = " + pctVal +
"\nBase UnitValue = " + baseVal +
"\nInch UnitValue = " + inchVal +
"\nPixel UnitValue = " + pixVal;
alert(msg);
app.preferences.rulerUnits = origRulerUnits;
CS2 bug?
CS2 bug?
There are some unitValue bugs in Photoshop. I don't know if this can be called a bug or just a limitation of using unitValues set to percent. But is seems that Photoshop is unable to convert directly from percent to pixels. It effect all version of Photoshop that use unitValues.
CS2 bug?
There is a bug with using UnitValue as reported here…
http://forums.adobe.com/message/3382995?tstart=0
http://forums.adobe.com/message/3382995?tstart=0
CS2 bug?
Mike Hale wrote:There are some unitValue bugs in Photoshop. I don't know if this can be called a bug or just a limitation of using unitValues set to percent. But is seems that Photoshop is unable to convert directly from percent to pixels. It effect all version of Photoshop that use unitValues.
I don't understand, why would PS be unable to convert a percent to pixels? It's a simple conversion if you know the total size of the document and the resolution.
I don't understand, why would PS be unable to convert a percent to pixels? It's a simple conversion if you know the total size of the document and the resolution.
CS2 bug?
xbytor wrote:You can write this
Code: Select allvar pixVal = pctVal.as("px");
as this
Code: Select allvar pixVal = UnitValue(pctVal.as("in"), "in").as("px");
to get around the limitation/bug.
This only works when the document's resolution is the default 72dpi.
In the code I posted...
50% of 10" is 5", and your code converts that to 360px. This is correct if the resolution of the 10" document is 72dpi (5 x 72 = 360), but what if the document's resolution is something else, like say 300 dpi? 50% of a 10" document at 300dpi would be 1500px.
I think the problem is the as('px') code can't know what document/resolution it's dealing with and simply uses the 72 pixels/inch default. It's not really a bug, it's a limitation of the class design. There's no way to specify both the size of a pixel and a total size, which are both needed for percent calculations. You can only set the UnitValue baseUnit to one or the other.
This is what I'm doing as a workaround. It's no problem because my code is only used with an active document anyway.
Code: Select allresult = Number(inVal * app.activeDocument.resolution);
Code: Select allvar pixVal = pctVal.as("px");
as this
Code: Select allvar pixVal = UnitValue(pctVal.as("in"), "in").as("px");
to get around the limitation/bug.
This only works when the document's resolution is the default 72dpi.
In the code I posted...
50% of 10" is 5", and your code converts that to 360px. This is correct if the resolution of the 10" document is 72dpi (5 x 72 = 360), but what if the document's resolution is something else, like say 300 dpi? 50% of a 10" document at 300dpi would be 1500px.
I think the problem is the as('px') code can't know what document/resolution it's dealing with and simply uses the 72 pixels/inch default. It's not really a bug, it's a limitation of the class design. There's no way to specify both the size of a pixel and a total size, which are both needed for percent calculations. You can only set the UnitValue baseUnit to one or the other.
This is what I'm doing as a workaround. It's no problem because my code is only used with an active document anyway.
Code: Select allresult = Number(inVal * app.activeDocument.resolution);
CS2 bug?
Code: Select allThis only works when the document's resolution is the default 72dpi.
Ahhhh,.. OK. One of my standard practices in my scripts is to always convert the doc res to 72ppi.
There are several other places in the PS DOM where this is implicitly assumed, so it is always
safer for me to switch to 72 at the beginning of the script and switch back to the original when
I am finished with the document.
Ahhhh,.. OK. One of my standard practices in my scripts is to always convert the doc res to 72ppi.
There are several other places in the PS DOM where this is implicitly assumed, so it is always
safer for me to switch to 72 at the beginning of the script and switch back to the original when
I am finished with the document.
CS2 bug?
xbytor wrote:Ahhhh,.. OK. One of my standard practices in my scripts is to always convert the doc res to 72ppi.
There are several other places in the PS DOM where this is implicitly assumed, so it is always
safer for me to switch to 72 at the beginning of the script and switch back to the original when
I am finished with the document.
Hadn't thought of that. Good idea!
There are several other places in the PS DOM where this is implicitly assumed, so it is always
safer for me to switch to 72 at the beginning of the script and switch back to the original when
I am finished with the document.
Hadn't thought of that. Good idea!