InDesign脚本将矩形背景颜色代码填充到文本框中

huangapple go评论65阅读模式
英文:

InDesign Script to Populate Rectangle BG Color Codes into TextBox

问题

I mostly work on creating Brand guidelines using InDesign where I have to add multiple brand colors. Each color is shown on a page as a rectangle background and a text box that shows the color codes with its Hex, RGB, and CMYK code.

It gets quite hectic as I have mostly 10-30 colors and manually does all these for each color.

InDesign脚本将矩形背景颜色代码填充到文本框中

I'm new to Javascript but somehow managed to get some color codes using JSX with the help of ChatGPT but I can't get CMYK color if the document type is RGB and if the document type is CMYK then I'm unable to get RGB.

I'm only interested in the color values present on the color picker, and I know depending on the document type CMYK values might be different, but that doesn't matter as of now.

Working on it on Windows PC so no Applescript option :(.

Here is my current code:

if (app.selection.length > 0) {
  for (var i = 0; i < app.selection.length; i++) {
    var selectedObject = app.selection[i];

    if (selectedObject instanceof Rectangle && selectedObject.fillColor !== null) {
      var fillColor = selectedObject.fillColor;

      if (fillColor.space === ColorSpace.RGB) {
        var hexCode = getHexCode(fillColor);
        var rgbCode = getRGBCode(fillColor);
        var cmykCode = "N/A"; // CMYK not applicable for RGB colors
        
        createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode);
      } else if (fillColor.space === ColorSpace.CMYK) {
        var hexCode = "N/A"; // Hex not applicable for CMYK colors
        var rgbCode = getRGBCode(fillColor);
        var cmykCode = getCMYKCode(fillColor);
        
        createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode);
      }
    }
  }
}

function createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode) {
  var frameWidth = 280; // Width of the text frame
  var frameHeight = 120; // Height of the text frame
  
  var topMargin = 40; // Top margin
  var leftMargin = 40; // Left margin

  // Calculate the coordinates for the bounding box
  var boundingBox = [
    selectedObject.geometricBounds[0] + topMargin, // Top position with margin
    selectedObject.geometricBounds[1] + leftMargin, // Left position with margin
    selectedObject.geometricBounds[0] + topMargin + frameHeight, // Bottom position with margin
    selectedObject.geometricBounds[1] + leftMargin + frameWidth // Right position with margin
  ];

  var textFrame = selectedObject.parentPage.textFrames.add({
    geometricBounds: boundingBox,
    contents: "RGB: " + rgbCode + "\nCMYK: " + cmykCode + "\nHex: " + hexCode,
    textFramePreferences: {
      autoSizingType: AutoSizingTypeEnum.OFF
    }
  });

  textFrame.fillColor = "None";
  textFrame.strokeColor = "None";

  var basicParagraphStyle = app.activeDocument.paragraphStyles.itemByName("Basic Paragraph");

  if (basicParagraphStyle.isValid) {
    textFrame.paragraphs.everyItem().appliedParagraphStyle = basicParagraphStyle;
  }

  textFrame.textFramePreferences.verticalJustification = VerticalJustification.CENTER_ALIGN;
}

function getHexCode(color) {
  var colorValues = color.colorValue;
  var hexCode = "#" + decimalToHex(colorValues[0]) + decimalToHex(colorValues[1]) + decimalToHex(colorValues[2]);
  return hexCode;
}

function decimalToHex(decimal) {
  var hex = Math.round(decimal).toString(16);
  return hex.length === 1 ? "0" + hex : hex;
}

function getRGBCode(color) {
  var colorValues = color.colorValue;
  var rgbCode = Math.round(colorValues[0]) + ", " + Math.round(colorValues[1]) + ", " + Math.round(colorValues[2]);
  return rgbCode;
}

function getCMYKCode(color) {
  var colorValues = color.colorValue;
  var cmykCode = colorValues[0] + "%, " + colorValues[1] + "%, " + colorValues[2] + "%, " + colorValues[3] + "%";
  return cmykCode;
}

I would appreciate any help in fixing the issue.

英文:

I mostly work on creating Brand guidelines using InDesign where I have to add multiple brand colors. Each color is shown on a page as a rectangle background and a text box that shows the color codes with its Hex, RGB, and CMYK code.

It gets quite hectic as I have mostly 10-30 colors and manually does all these for each color.

InDesign脚本将矩形背景颜色代码填充到文本框中

I'm new to Javascript but somehow managed to get some color codes using JSX with the help of ChatGPT but I can't get CMYK color if the document type is RGB and if the document type is CMYK then I'm unable to get RGB.

I'm only interested in the color values present on the color picker and I know depending on the document type CMYK values might be different but that doesn't matter as of now.

Working on it on Windows PC so no Applescript option :(.

Here is my current code:

if (app.selection.length &gt; 0) {
for (var i = 0; i &lt; app.selection.length; i++) {
var selectedObject = app.selection[i];
if (selectedObject instanceof Rectangle &amp;&amp; selectedObject.fillColor !== null) {
var fillColor = selectedObject.fillColor;
if (fillColor.space === ColorSpace.RGB) {
var hexCode = getHexCode(fillColor);
var rgbCode = getRGBCode(fillColor);
var cmykCode = &quot;N/A&quot;; // CMYK not applicable for RGB colors
createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode);
} else if (fillColor.space === ColorSpace.CMYK) {
var hexCode = &quot;N/A&quot;; // Hex not applicable for CMYK colors
var rgbCode = getRGBCode(fillColor);
var cmykCode = getCMYKCode(fillColor);
createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode);
}
}
}
}
function createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode) {
var frameWidth = 280; // Width of the text frame
var frameHeight = 120; // Height of the text frame
var topMargin = 40; // Top margin
var leftMargin = 40; // Left margin
// Calculate the coordinates for the bounding box
var boundingBox = [
selectedObject.geometricBounds[0] + topMargin, // Top position with margin
selectedObject.geometricBounds[1] + leftMargin, // Left position with margin
selectedObject.geometricBounds[0] + topMargin + frameHeight, // Bottom position with margin
selectedObject.geometricBounds[1] + leftMargin + frameWidth // Right position with margin
];
var textFrame = selectedObject.parentPage.textFrames.add({
geometricBounds: boundingBox,
contents: &quot;RGB: &quot; + rgbCode + &quot;\nCMYK: &quot; + cmykCode + &quot;\nHex: &quot; + hexCode,
textFramePreferences: {
autoSizingType: AutoSizingTypeEnum.OFF
}
});
textFrame.fillColor = &quot;None&quot;;
textFrame.strokeColor = &quot;None&quot;;
var basicParagraphStyle = app.activeDocument.paragraphStyles.itemByName(&quot;Basic Paragraph&quot;);
if (basicParagraphStyle.isValid) {
textFrame.paragraphs.everyItem().appliedParagraphStyle = basicParagraphStyle;
}
textFrame.textFramePreferences.verticalJustification = VerticalJustification.CENTER_ALIGN;
}
function getHexCode(color) {
var colorValues = color.colorValue;
var hexCode = &quot;#&quot; + decimalToHex(colorValues[0]) + decimalToHex(colorValues[1]) + decimalToHex(colorValues[2]);
return hexCode;
}
function decimalToHex(decimal) {
var hex = Math.round(decimal).toString(16);
return hex.length === 1 ? &quot;0&quot; + hex : hex;
}
function getRGBCode(color) {
var colorValues = color.colorValue;
var rgbCode = Math.round(colorValues[0]) + &quot;, &quot; + Math.round(colorValues[1]) + &quot;, &quot; + Math.round(colorValues[2]);
return rgbCode;
}
function getCMYKCode(color) {
var colorValues = color.colorValue;
var cmykCode = colorValues[0] + &quot;%, &quot; + colorValues[1] + &quot;%, &quot; + colorValues[2] + &quot;%, &quot; + colorValues[3] + &quot;%&quot;;
return cmykCode;
}

I would appreciate any help in fixing the issue.

答案1

得分: 1

以下是您要翻译的内容:

// create the temp color with the same properties as 'fillColor'
var temp_color = app.activeDocument.colors.add({
  name:       fillColor.name + '_temp',
  colorValue: fillColor.colorValue,
  space:      fillColor.space,
});

if (fillColor.space === ColorSpace.RGB) {
  var hexCode = getHexCode(fillColor);
  var rgbCode = getRGBCode(fillColor);
  temp_color.space = ColorSpace.CMYK;      // convert the temp color into CMYK
  var cmykCode = getCMYKCode(temp_color);  // get the numbers
  createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode);
}
else if (fillColor.space === ColorSpace.CMYK) {
  temp_color.space = ColorSpace.RGB;       // convert the temp color into RGB
  var hexCode = getHexCode(temp_color);    // get the numbers
  var rgbCode = getRGBCode(temp_color);    // get the numbers
  var cmykCode = getCMYKCode(fillColor);
  createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode);
}

temp_color.remove(); // remove the temp color

或者,您可以使用以下代码替换上面的代码:

// get the properties from 'fillColor'
var temp_color_properties = fillColor.properties;

// create the temp CMYK color
temp_color_properties.name = fillColor.name + '_temp_cmyk';
var temp_cmyk_color = app.activeDocument.colors.add(temp_color_properties);
temp_cmyk_color.space = ColorSpace.CMYK;

// create the temp RGB color
temp_color_properties.name = fillColor.name + '_temp_rgb';
var temp_rgb_color = app.activeDocument.colors.add(temp_color_properties);
temp_rgb_color.space = ColorSpace.RGB;

// get the numbers
var hexCode  = getHexCode(temp_rgb_color);
var rgbCode  = getRGBCode(temp_rgb_color);
var cmykCode = getCMYKCode(temp_cmyk_color);

// remove the temp colors
temp_cmyk_color.remove();
temp_rgb_color.remove();

createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode);

并且,您可能需要像对RGB值一样对CMYK值添加Math.round()

结果:

InDesign脚本将矩形背景颜色代码填充到文本框中

更新。 该脚本使用文档的当前测量单位。因此,如果您使用英寸,可能会得到以下奇怪的结果:

InDesign脚本将矩形背景颜色代码填充到文本框中

您可以在代码的开头使用以下代码将单位临时更改为像素:

var doc = app.activeDocument;
var orig_x_units = doc.viewPreferences.horizontalMeasurementUnits;
var orig_y_units = doc.viewPreferences.verticalMeasurementUnits;
doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;
doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;

然后,在脚本的结尾使用以下代码将单位更改回原始单位:

doc.viewPreferences.horizontalMeasurementUnits = orig_x_units;
doc.viewPreferences.verticalMeasurementUnits = orig_y_units;
英文:

As far as I can tell, you can create the temp color with the same values, convert it into any space (CMYK or RGB) and then get the numbers from this temp color:

// create the temp color with the same properties as &#39;fillColor&#39;
var temp_color = app.activeDocument.colors.add({
name:       fillColor.name + &#39;_temp&#39;,
colorValue: fillColor.colorValue,
space:      fillColor.space,
});
if (fillColor.space === ColorSpace.RGB) {
var hexCode = getHexCode(fillColor);
var rgbCode = getRGBCode(fillColor);
temp_color.space = ColorSpace.CMYK;      // convert the temp color into CMYK
var cmykCode = getCMYKCode(temp_color);  // get the numbers
createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode);
}
else if (fillColor.space === ColorSpace.CMYK) {
temp_color.space = ColorSpace.RGB;       // convert the temp color into RGB
var hexCode = getHexCode(temp_color);    // get the numbers
var rgbCode = getRGBCode(temp_color);    // get the numbers
var cmykCode = getCMYKCode(fillColor);
createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode);
}
temp_color.remove(); // remove the temp color

Or you can replace the the code above with these lines:

// get the properties from &#39;fillColor&#39;
var temp_color_properties = fillColor.properties;
// create the temp CMYK color
temp_color_properties.name = fillColor.name + &#39;_temp_cmyk&#39;;
var temp_cmyk_color = app.activeDocument.colors.add(temp_color_properties);
temp_cmyk_color.space = ColorSpace.CMYK;
// create the temp RGB color
temp_color_properties.name = fillColor.name + &#39;_temp_rgb&#39;;
var temp_rgb_color = app.activeDocument.colors.add(temp_color_properties);
temp_rgb_color.space = ColorSpace.RGB;
// get the numbers
var hexCode  = getHexCode(temp_rgb_color);
var rgbCode  = getRGBCode(temp_rgb_color);
var cmykCode = getCMYKCode(temp_cmyk_color);
// remove the temp colors
temp_cmyk_color.remove();
temp_rgb_color.remove();
createColorCodeTextFrame(selectedObject, hexCode, rgbCode, cmykCode);

And probably you need to add Math.round() to CMYK values as well as it's done for RGB values.

Results:

InDesign脚本将矩形背景颜色代码填充到文本框中

Update. The script uses current measuring units of the document. So if you're using inches it can get you the weird results like this:

InDesign脚本将矩形背景颜色代码填充到文本框中

You can temporary change units to pixels with these lines at the start of your code:

var doc = app.activeDocument;
var orig_x_units = doc.viewPreferences.horizontalMeasurementUnits;
var orig_y_units = doc.viewPreferences.verticalMeasurementUnits;
doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;
doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;

And then change the units back to orinal with these lines at the end of your script:

doc.viewPreferences.horizontalMeasurementUnits = orig_x_units;
doc.viewPreferences.verticalMeasurementUnits = orig_y_units;

huangapple
  • 本文由 发表于 2023年6月6日 00:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定