Skip to main content
Inspiring
October 31, 2024
Answered

Get the clipping path flatness.

  • October 31, 2024
  • 2 replies
  • 736 views

I'm trying to find the flatness value of the clipping path.

The code below returns a number but it needs to be converted.

How to get the real value of the flatness ?

 

alert (getClippingPathFlatness());

function getClippingPathFlatness(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet( ref );
var clippingPath = desc.getObjectValue(charIDToTypeID("Clpg"));
return  clippingPath.getInteger(charIDToTypeID("ClpF" ));
};

This topic has been closed for replies.
Correct answer Stephen Marsh

@Panchromatic 

 

That was a nice exercise!

 

I looked for a pattern in the numbers:

 

0.2 = 13107
0.5 = 32768

0.7 = 45875
1.0 = 65536
1.5 = 98304

1.7 = 111411
2.0 = 131072

 

I then reverse-engineered that into the following, which appears to work consistently:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-the-clipping-path-flatness/td-p/14953621
*/

alert("Clipping Path Flatness: " + getClippingPathFlatness() + " device pixels");

function getClippingPathFlatness() {
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var desc = executeActionGet(ref);
    var clippingPath = desc.getObjectValue(charIDToTypeID("Clpg"));
    // 65536 = 1.0 as the internal programming value, range of 0.2 - 100 scale in device pixels
    return (clippingPath.getInteger(charIDToTypeID("ClpF")) / 65536).toFixed(1);
}

 

Edit: Not being a programmer, I asked AI why 65536 was significant and this was the reply –

 

In programming, especially when dealing with digital graphics and certain encoding schemes, 65536 might represent 1.0 due to fixed-point arithmetic or specific scaling conventions.
Here's the scoop: 65536 is 2^16, or a number representing the range of values in 16-bit systems. When graphics systems use 16-bit fixed-point numbers, 65536 is often the scaling factor to move between integer representation and floating-point representation.
So, in your context, the value 65536 could be representing the normalized floating-point value 1.0. This mapping ensures precision in representing and manipulating values within the limited range of an integer but allows for smooth transitions when converting to floats. It's a nifty way to ensure compatibility and precision.

 

2 replies

Stephen Marsh
Community Expert
Community Expert
November 1, 2024

I'd like to be able to apply that retrieved flatness value to a clipping path, however, I'm not having any luck 

 

quote

Error: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.

- The command “Set” is not currently available.

 

setClippingPathFlatness(getClippingPathFlatness());

 

function setClippingPathFlatness(theValue) {
var idset = stringIDToTypeID("set");
var desc1222 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref75 = new ActionReference();
var idpath = stringIDToTypeID("path");
var idclippingPath = stringIDToTypeID("clippingPath");
ref75.putProperty(idpath, idclippingPath);
desc1222.putReference(idnull, ref75);
var idto = stringIDToTypeID("to");
var desc1223 = new ActionDescriptor();
var idpath = stringIDToTypeID("path");
var ref76 = new ActionReference();
var idpath = stringIDToTypeID("path");
var idordinal = stringIDToTypeID("ordinal");
var idtargetEnum = stringIDToTypeID("targetEnum");
ref76.putEnumerated(idpath, idordinal, idtargetEnum);
desc1223.putReference(idpath, ref76);
var idflatness = stringIDToTypeID("flatness");
desc1223.putDouble(idflatness, theValue); // Set the flatness
var idclippingPathEPS = stringIDToTypeID("clippingPathEPS");
desc1222.putObject(idto, idclippingPathEPS, desc1223);
executeAction(idset, desc1222, DialogModes.NO);
}

 

Stephen Marsh
Community Expert
Community Expert
November 2, 2024

Hmm... As usual, it appears that I have forgotten more than I have remembered!  :]

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/create-clipping-path-from-current-layer-s-vector-or-layermask/m-p/10475301

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-for-conversion-to-clipping-path-and-running-on-a-batch/td-p/14435255

 

EDIT:

After a lot of trial and error, I had to force the flatness value to a number for it to be accepted when setting the value.

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-the-clipping-path-flatness/td-p/14953621
*/

var theFlatnessValue = Number(getClippingPathFlatness()); // Force the value to a number
alert("Clipping Path Flatness: " + theFlatnessValue + " device pixels");

function getClippingPathFlatness() {
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var desc = executeActionGet(ref);
    var clippingPath = desc.getObjectValue(charIDToTypeID("Clpg"));
    // 65536 = 1.0 as the internal programming value, range of 0.2 - 100 scale in device pixels
    return (clippingPath.getInteger(charIDToTypeID("ClpF")) / 65536).toFixed(1);
}
Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
November 1, 2024

@Panchromatic 

 

That was a nice exercise!

 

I looked for a pattern in the numbers:

 

0.2 = 13107
0.5 = 32768

0.7 = 45875
1.0 = 65536
1.5 = 98304

1.7 = 111411
2.0 = 131072

 

I then reverse-engineered that into the following, which appears to work consistently:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-the-clipping-path-flatness/td-p/14953621
*/

alert("Clipping Path Flatness: " + getClippingPathFlatness() + " device pixels");

function getClippingPathFlatness() {
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var desc = executeActionGet(ref);
    var clippingPath = desc.getObjectValue(charIDToTypeID("Clpg"));
    // 65536 = 1.0 as the internal programming value, range of 0.2 - 100 scale in device pixels
    return (clippingPath.getInteger(charIDToTypeID("ClpF")) / 65536).toFixed(1);
}

 

Edit: Not being a programmer, I asked AI why 65536 was significant and this was the reply –

 

In programming, especially when dealing with digital graphics and certain encoding schemes, 65536 might represent 1.0 due to fixed-point arithmetic or specific scaling conventions.
Here's the scoop: 65536 is 2^16, or a number representing the range of values in 16-bit systems. When graphics systems use 16-bit fixed-point numbers, 65536 is often the scaling factor to move between integer representation and floating-point representation.
So, in your context, the value 65536 could be representing the normalized floating-point value 1.0. This mapping ensures precision in representing and manipulating values within the limited range of an integer but allows for smooth transitions when converting to floats. It's a nifty way to ensure compatibility and precision.

 

Inspiring
November 2, 2024

Your solution gives the correct value.

I specially appraised the detailed answer with the edit.

Thanks again for your help.

Stephen Marsh
Community Expert
Community Expert
November 2, 2024
quote

Your solution gives the correct value.

I specially appraised the detailed answer with the edit.

Thanks again for your help.


By @Panchromatic

 

You're welcome, I have encountered this "magic number" before, however, I can never remember the where or why. It's just one of those internal workings that are not usually found until some esoteric scripting scenario finds them.