• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Retrieving all swatch values and names via JSX

Enthusiast ,
May 27, 2021 May 27, 2021

Copy link to clipboard

Copied

I need to verify if a user has a given swatch by name and value in JSX.

 

Just to clarify, not asking how to create a swatch or how to parse .aco files (that's practically all I could find relating to this online), but instead how to query a list of all document swatches then construct an array containing objects with key/value pairs for name and color of each swatch.

 

I came up with a decent enough solution, but with the 100+ default swatches that PS has, it takes longer than I'd like to process:

function getDocumentSwatches() {
  var ref = new ActionReference();
  ref.putEnumerated(
    charIDToTypeID("capp"),
    charIDToTypeID("Ordn"),
    charIDToTypeID("Trgt")
  );
  var swatchList = executeActionGet(ref)
    .getList(stringIDToTypeID("presetManager"))
    .getObjectValue(1);
  var nameList = swatchList.getList(charIDToTypeID("Nm  "));
  var result = [];
  for (var m = 0; m < nameList.count; m++) {
    var temp = {
      name: nameList.getString(m),
      possibleValues: getColorInfoFromSwatch(m),
    };
    result.push(temp);
  }
  return JSON.stringify(result);
}

function getColorInfoFromSwatch(index) {
  try {
    var foreground = app.foregroundColor;
    var tempDesc = new ActionDescriptor();
    var tempRef = new ActionReference();
    tempRef.putIndex(stringIDToTypeID("colors"), index + 1);
    tempDesc.putReference(stringIDToTypeID("null"), tempRef);
    executeAction(stringIDToTypeID("select"), tempDesc, DialogModes.NO);
    var swatchColor = app.foregroundColor;
    app.foregroundColor = foreground;
    var result = getPossibleColorMatchStrings(swatchColor);
    return result;
  } catch (e) {
    alert(e);
    return null;
  }
}

function getPossibleColorMatchStrings(color) {
  var omittedKeys = ["model", "typename", "nearestWebColor", "hexValue"];
  var omittedRX = new RegExp(omittedKeys.join("|"), "i");
  var type = "";
  var result = [];
  for (var key in color)
    if (!omittedRX.test(key)) {
      var subString = key.toUpperCase() + "(";
      for (var subKey in color[key])
        if (!omittedRX.test(subKey))
          subString += Math.round(color[key][subKey]) + ", ";
      subString = subString.replace(/,\s?$/, "") + ")";
      result.push(subString);
    }
  result.push("#" + color.rgb.hexValue);
  return result;
}

var test = getDocumentSwatches();
alert(test);

The only way I can figure to get the color information is to set the swatch as app.foregroundColor. This causes the model of any given swatch to report as the document model and not accurately per-swatch model (causing CMYK swatches to report as RGB), meaning that I can't dynamically use the color.model to create a single string and am forced to create an array of possible values instead (which I'd then match to later).

 

Is there a better way to do this? Right now I construct an array like this:

[
  {
    name: "RGB Red",
    possibleValues: [
      "RGB(255, 0, 0)",
      "#FF0000",
      "CMYK(0, 0, 0, 0)",
      ...
    ]
  },
  ...
]


Ideally I'd like one like so:

[
  {
    name: "RGB Red",
    value: "RGB(255, 0, 0)", // this would be ideal
    group: "RGB" // this would be ideal
  },
  ...
]

 

TOPICS
Actions and scripting

Views

373

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
People's Champ ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

LATEST

A bit unclear.

And why don't you focus on the model?

 

function getPossibleColorMatchStrings(color) 
    {
    try {
        switch (color.model)
            {
            case ColorModel.RGB:       return "RGB("+color.rgb.red+","+color.rgb.green+","+color.rgb.blue+")"; 
            case ColorModel.LAB:       return "LAB("+color.lab.l+","+color.lab.a+","+color.lab.b+")"; 
            case ColorModel.HSB:       return "HSB("+color.hsb.hue+","+color.hsb.saturation+","+color.hsb.brightness+")"; 
            case ColorModel.CMYK:      return "CMYK("+color.cmyk.cyan+","+color.cmyk.magenta+","+color.cmyk.yellow+","+color.cmyk.black+")"; 
            case ColorModel.GRAYSCALE: return "GRAY("+color.gray.gray+")"; 
            }

        return "undefined";
        }
    catch(e) { throw(e); }
    }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines