Skip to main content
Inspiring
November 26, 2024
Answered

Scripting Cycle Through Cyan Shades

  • November 26, 2024
  • 2 replies
  • 2354 views

Why does my scipt crash when I delete the two occurances of /**/ (leaving in the code) and deleting the single line of code with the // trailing the cyan variable?

var i = 0;
var ii = 0;
var y = 0;
var x = 0;
var aDoc = app.documents.add();
for (; i < 374; ) {  
  var Rect = aDoc.pathItems.rectangle(y, x, 36, -36);  
  Rect.stroked = false;
  Rect.filled = true;
  var itemColor = new CMYKColor();
  /*if (i = 0) {
    itemColor.cyan = 0;
  }*/
  itemColor.cyan = 97;  //Why deleting this line and uncommenting code doesn't work???
  itemColor.magenta = 0;
  itemColor.yellow = 0;
  itemColor.black = 0;
  Rect.fillColor = itemColor;
  /*if (x > 0 && x < 594 && itemColor.cyan < 100) {
    itemColor.cyan = itemColor.cyan + 1;
  }*/
  var ii = ii + 1;
  var i = i + 1;
  var x = x + 36;
  if (ii == 17) {
    var ii = 0;
    var x = 0;
    var y = y + 36;
    }
}

 You can run the script as is and it doesn't crash and makes nice even 1/2" squares on 8.5x11 however when I try to impliment the part of code to change through 0 - 100 shades of cyan something breaks.  Maybe I'm not getting to the var in the for loop from the if statements? 

This topic has been closed for replies.
Correct answer ShantiShaun

I found my solution was to make a separate global variable for the cyan color.  I have corrected my code to share with everyone...

var i = 0;
var ii = 0;
var y = 0;
var x = 0;
var ccount = 0;
var aDoc = app.documents.add();
for (; i < 374; ) {  
  var Rect = aDoc.pathItems.rectangle(y, x, 36, -36);  
  Rect.stroked = false;
  Rect.filled = true;
  var itemColor = new CMYKColor();
  itemColor.cyan = ccount;
  itemColor.magenta = 0;
  itemColor.yellow = 0;
  itemColor.black = 0;
  if (x > 0 && x < 594 && ccount < 100) {
    var ccount = ccount + 1;
  }
  Rect.fillColor = itemColor;
  var ii = ii + 1;
  var i = i + 1;
  var x = x + 36;
  if (ii == 17) {
    var ii = 0;
    var x = 0;
    var y = y + 36;
    }
}

2 replies

ShantiShaunAuthorCorrect answer
Inspiring
November 26, 2024

I found my solution was to make a separate global variable for the cyan color.  I have corrected my code to share with everyone...

var i = 0;
var ii = 0;
var y = 0;
var x = 0;
var ccount = 0;
var aDoc = app.documents.add();
for (; i < 374; ) {  
  var Rect = aDoc.pathItems.rectangle(y, x, 36, -36);  
  Rect.stroked = false;
  Rect.filled = true;
  var itemColor = new CMYKColor();
  itemColor.cyan = ccount;
  itemColor.magenta = 0;
  itemColor.yellow = 0;
  itemColor.black = 0;
  if (x > 0 && x < 594 && ccount < 100) {
    var ccount = ccount + 1;
  }
  Rect.fillColor = itemColor;
  var ii = ii + 1;
  var i = i + 1;
  var x = x + 36;
  if (ii == 17) {
    var ii = 0;
    var x = 0;
    var y = y + 36;
    }
}
m1b
Community Expert
Community Expert
November 27, 2024

Great work @ShantiShaun! It works really well.

 

Just for extra learning, I've refactored your code a bit to improve readaiblity and structure. Some variables weren't really needed with the new structure. I hope it might be interesting.

- Mark

 

 

(function () {

    // Configuration variables
    var rowWidth = 17; // Number of rectangles per row
    var cellSize = 36; // Size of each rectangle
    var totalCount = 374; // Total number of rectangles

    // Create a new document
    var newDoc = app.documents.add();

    // Initialize variables
    var totalRows = Math.ceil(totalCount / rowWidth);

    // Loop over rows
    for (var r = 0; r < totalRows; r++) {

        // Calculate the row's vertical position
        var y = r * cellSize;

        // Loop over cells in the row
        for (var i = 0; i < rowWidth && cellIndex < totalCount; i++) {

            // Calculate the cell's horizontal position
            var x = i * cellSize,
                cellIndex = r * rowWidth + i;

            // Create a rectangle
            var rect = newDoc.pathItems.rectangle(y, x, cellSize, -cellSize);

            // Apply fill color
            setAppearance(rect, cellIndex);

        }
    }

})();

// Function to set fill color based on the current count
function setAppearance(item, cyanValue) {

    if (
        cyanValue < 0
        || cyanValue > 100
    )
        cyanValue = 100;

    var color = new CMYKColor();
    color.cyan = cyanValue;
    color.magenta = 0;
    color.yellow = 0;
    color.black = 0;

    item.stroked = false;
    item.filled = true;
    item.fillColor = color;

};

EDIT 2024-11-30: fixed a bug found by @renél80416020  and @ShantiShaun. Sorry, going to fast!

 

Inspiring
November 29, 2024

Mark, Thanks for the efficient suggestion.  This looks like a good alternative for better code writing.  This is a good reference for anyone who has a need to start cleaning up code.  My purposes I've left my code as is and expanded on it a little  bit.  I've got it going now with all outside squares larger to fill a bleed area of 9pts.

 

I have a question related to scripting.  There is a sample script included in paticular "C:\Program Files\Adobe\Adobe Illustrator 2025\Scripting\Sample Scripts\JavaScript\MultiArtboards\CreateArtboards.jsx" I was wondering do you get the same output if you zoom out and look with the large rectangle not aligning to the pages and the random stars going up north off the rectangle?  Please see the screenshot attachment and I'm wondering why coordinates seem to be wonky in this example and during the development of my script it seemed Illustrator would not make it's mind up if a coordinate was going to be (top left origin) or (center origin), and also it seemed while I was developing my script that Illustrator sometimes would not make it's mind up if my coordinates were going to be interpreted as Global or Relative to my last artboard when adding artboards.

Happy Thanksgiving.

m1b
Community Expert
Community Expert
November 26, 2024

Hi @ShantiShaun the first thing that stands out is that you are—probably accidentally!—assigning i rather than comparing.

 

Try changing

if (i = 0) {

to

if (i === 0) {

 

There might be other issues, but that's probably blocking you for now.

 - Mark

Inspiring
November 26, 2024

m1b, Thanks.  I found this and everything else.  solved.