Using extendscript (javascript) how can I get the color values from the Photoshop color table - photoshop

I'm writing a Photoshop script in extendscript/javascript and I'm trying to verify that the document is using just one color (plus transparency). What I would like to do is change the document mode to Indexed Color and then get the values in the color table.
I have successfully changed the document mode to Indexed Color but can't figure out how to access the color table or the color values inside of it.
My working alternative is to use a colorSampler to compare the values of each pixel, but that can take a couple of minutes to run on larger documents and speed is an issue for this project.
Please let me know if there is a way to access the color table or if you see a way to reduce the time it takes to run this function.
function sample_color(doc, sample_rate) {
var status = 'PASS'
var color_sampler = doc.colorSamplers.add([0,0])
var color_val = false //first (and hopefully only) color value in the document
var broke = false
for (x=1; x < doc.width; x+=sample_rate){
if (broke){
break
}
for (y=1; y < doc.height; y+=sample_rate){
color_sampler.move([UnitValue(x, 'px'), UnitValue(y, 'px')])
try{
var color = color_sampler.color //color of the current pixel
} catch(e) {
var color = false //color_sampler.color fails if the pixel is transparent
}
if (color != false){
if (color_val != false){
if (!color.isEqual(color_val)){
status = 'FAIL'
broke = true
break
}
} else {
color_val = color
}
}
}
}
color_sampler.remove()
return status
}

xbytor has written a couple of scripts for accessing colour tables. This link may be of use to you.

Related

Custom CollapsingTopAppBar Jetpack Compose

The essence of the problem is that I want to write my own version of the AppBar that would include content as another Compose function. After looking at the source code of the current CollapsingTopAppBar implementation, I saw the following lines:
#Composable
private fun TwoRowsTopAppBar(
...
scrollBehavior: TopAppBarScrollBehavior?
) {
...
val pinnedHeightPx: Float = 64.dp
val maxHeightPx: Float = 152.dp
LocalDensity.current.run {
pinnedHeightPx = pinnedHeight.toPx()
maxHeightPx = maxHeight.toPx()
}
// Sets the app bar's height offset limit to hide just the bottom title area and keep top title
// visible when collapsed.
SideEffect {
if (scrollBehavior?.state?.heightOffsetLimit != pinnedHeightPx - maxHeightPx) {
scrollBehavior?.state?.heightOffsetLimit = pinnedHeightPx - maxHeightPx
}
}
...
Surface(...) {
Column {
TopAppBarLayout(
...
heightPx = pinnedHeightPx
...
)
TopAppBarLayout(
...
heightPx = maxHeightPx - pinnedHeightPx + (scrollBehavior?.state?.heightOffset
?: 0f),
...
)
}
}
}
As I understand it, scrollBehavior is used to handle the collapse and expansion behavior. In the current implementation, just constant values are put in heightOffsetLimit. And since I need my appbar implementation to be able to contain content of any size, I need to somehow know the size of this content in advance and put this value in heightOffsetLimit.
I have already written the code for my AppBar, so that it also contains content. But since I can't pass the height value of the content to scrollBehavior, the AppBar doesn't collapse to the end.
you need to calculate the height that the appbar will have before drawing it into the screen. I have followed this issue and solved my problem with the last solution. hope it helps:
Get height of element Jetpack Compose
use the content you can put (ex. an image or a huge text) as the MainContent
use your appbar as the DependentContent and use the size given in lambda to give the height to your appbar
finally set placeMainContent false as I believe you don't need to draw the image (or any other composable) directly in a box
and you will good to go

Adobe Photoshop Scripting - How to Select Bounding Box Around Current Selection?

Does anyone know whether it's possible, in Photoshop extend script, to convert an irregular selection (e.g. magic wand tool selection) into a rectangular selection encompassing the top, left, bottom and right bounds of the selection?
Here it is, I have documented the code so you can modify it later if you need. Also, check page 166 and following of Photoshop's JS reference manual, you may read more about selections - you can set feather, extend/intersect/etc. the selection if you need to.
Made for CS6, should work with latter.
#target photoshop
if (documents.length == 0) {
alert("nothing opened");
} else {
// start
//setup
var file = app.activeDocument;
var selec = file.selection;
//run
var bnds = selec.bounds; // get the bounds of current selection
var // save the particular pixel values
xLeft = bnds[0],
yTop = bnds[1],
xRight = bnds[2],
yBottom = bnds[3];
var newRect = [ [xLeft,yTop], [xLeft,yBottom], [xRight,yBottom], [xRight,yTop] ]; // set coords for selection, counter-clockwise
selec.deselect;
selec.select(newRect);
// end
}

Windows 10 Mobile height of soft keyboard

Does anybody know how to move content of the page (maybe set relative margins or something like that) when soft keyboard is shown.
Here is the example page from my application.
So I want when the user starts typing a phone number in the text box the bottom button will be shown above the soft keyboard. As a result I want something like that:
P.S: Sorry about Russian language on the screens.
It's kind of tricky, but as I've tried should work. I've used InputPane's showing and hiding events to change the translate transform of the button. In page's constructor I've added such code:
Windows.UI.ViewManagement.InputPane.GetForCurrentView().Showing += (s, args) =>
{
GeneralTransform gt = loginButton.TransformToVisual(this);
Point buttonPoint = gt.TransformPoint(new Point(0, loginButton.RenderSize.Height - 1));
var trans = new TranslateTransform { Y = -(buttonPoint.Y - args.OccludedRect.Top) };
loginButton.RenderTransform = trans;
args.EnsuredFocusedElementInView = true;
};
Windows.UI.ViewManagement.InputPane.GetForCurrentView().Hiding += (s, args) =>
{
var trans = new TranslateTransform { Y = 0 };
loginButton.RenderTransform = trans;
args.EnsuredFocusedElementInView = false;
};
You only have to remember that InputPane is for the whole app - once you leave the page, you will probably have to unsubscribe from those events, otherwise you will likely get exceptions.

InDesign style variable

Is there a way to use variables in styles (script?) to share styles with some variations, like color?
Example: I am laying out a book, with multiple chapters. Each chapter is an InDesign document. I would like to use common styles for all the documents in the book, but they would vary in color. So instead of having multiple objects styles like: RoundedBox-red, RoundedBox-blue etc, I would only have one style, RoundedBox, and just input the value of the color variable somewhere…
Do you have a reason to examine only rectangles, ovals, and polygons? If not, you can use pageItems. Ditch the shape selection and the switch, and use:
shapes = myDoc.allPageItems;
for (var i=0; i<shapes.length; i++)
{
if (shapes[i].appliedObjectStyle.name === oldStyle.name)
{
shapes[i].applyObjectStyle( newStyle );
}
}
Since you have each chapter in a separate document, you could also just change the definition of your object style:
oldStyle.fillColor = newSwatch;
so you don't have to loop over the actual objects. Not tested but it should work.
Well, I found out. The hardest part was really to find a good documentation for the Indesign javascript scripting API... Adobe's documentation is either hard to find or lacking. In addition, they publish everything as PDFs, which IMHO is really annoying. I found a good online documentation for CS6. I am working on CC but everything I used seems the same. Anyhow, I have created the following script, very incomplete and not perfect, but works for me...
// Setup the dialog UI
var myDialog = app.dialogs.add({
name: "Style Variables",
canCancel: true
});
// I usually never use 'with', but this is how it is done
// in Adobe's documentation...
with(myDialog.dialogColumns.add()) {
staticTexts.add({staticLabel: "Main Color swatch name:"});
staticTexts.add({staticLabel: "Style to replace:"});
staticTexts.add({staticLabel: "Replace style with:"});
staticTexts.add({staticLabel: "Choose shape type to target:"});
}
with(myDialog.dialogColumns.add()){
var swatchField = textEditboxes.add({editContents:'', minWidth:180}),
oldStyleField = textEditboxes.add({editContents:'', minWidth:180}),
newStyleField = textEditboxes.add({editContents:'', minWidth:180}),
shapeTypeField = dropdowns.add({stringList:['Rectangles', 'Ovals', 'Polygons']}); // Defaults to rectangles
}
// Get the user input and do stuff with it
var myResult = myDialog.show();
if (myResult === true)
{
var docs = app.documents,
myDoc = docs[0],
allStyles = myDoc.objectStyles,
oldStyle = allStyles.itemByName(oldStyleField.editContents),
newStyle = allStyles.itemByName(newStyleField.editContents),
swatches = app.documents[0].swatches,
newSwatch = swatches.itemByName(swatchField.editContents),
shapes;
// Get the shape type we are targetting:
switch(shapeTypeField.selectedIndex)
{
case 0:
shapes = myDoc.rectanges;
break;
case 1:
shapes = myDoc.ovals;
break;
case 2:
shapes = myDoc.polygons;
break;
default:
shapes = myDoc.rectangles;
}
// Set the base style color to the user chosen swatch:
newStyle.fillColor = newSwatch;
for (var i=0; i<shapes.length; i++)
{
if (shapes[i].appliedObjectStyle.name === oldStyle.name)
{
shapes[i].applyObjectStyle( newStyle );
}
}
}
else
{
alert('Script cancelled, nothing was done.');
}
// Destroy dialog box
myDialog.destroy();

updateSeries during onclick event of a chart column works as expected only when debugging in Chrome

Using dojo, I create a chart with ColumnsPlot.
The chart is created and displayed the way I want it.
In order to handle a click on a column and to change the color of the clicked column I have the code below:
var handler = barChart.connectToPlot("default", function (event) {
if (event.type == "onclick") {
var currentSeriesData = event.run.data;
barChart.updateSeries("Yearly Offense Count", reverseColors(currentSeriesData, event.index));
barChart.render();
}
});
And the reverseColors is:
reverseColors = function (data, index) {
var tempData = [];
for (var i = 0; i < data.length; i++) {
tempData.push(data[i]);
if (i == index) {
tempData[i].fill = "red";
} else {
tempData[i].fill = "green";
}
}
console.log(i + " - " + tempData[i].fill);
return tempData;
}
I am testing in Google and using the Developers Tools to debug. If I set a break point inside the if statement and debug the app, the barChart is updated and the clicked columns get a red color, and all the others are green (as desired).
If I am not debugging, the barChart is updated, but all columns are green (originally chart is created with all green columns except one red column).
The reverseColors function returns the data in the correct format and wiht the correct fill while debugging, and when not debugging in the console I have 1 red and the rest of the items are green, as expected. But my chart has only green columns.
What am I messing up?
Dojo beginner!
Edit: Doesn't work in Chrome, works once in a blue moon in Firefox, and works fine is IE8.
The chart had a Highlight defined.
Removing the Highlight solved the problem.