Color Combination Adobe Illustrator Script

Colors are a big part of it and their combinations are very valuable. There are fewer and fewer brands using two colors today. They can use whatever color they want to use at that moment and they never shy away from it. The valuable thing for designers here is to make the color tones work in harmony with other colors.

Original?

The original source is actually a gradient solution: Gradient matrix generator for Adobe Illustrator

It is a very well written code but there is no non-gradient version. I also added the tabs to change the background and foreground shapes. Of course, I did not do it all by myself but thanks to ChatGPT. I did not succeed, it still has bugs, it will never be 100% good :(

Thanks again to the owner of the original code \ (•◡•) /

How?

Very simply, the script can offer this process very easily. The working logic is as follows:

  1. Save the following script in ".jsx" format

  2. Select the shapes in which you sort the colors

  3. Run the script in Adobe Illustrator

Is there a limit?

The limit is only as much as you want.

Code

//#target illustrator

var doc = app.activeDocument;
var selectedItems = doc.selection;
var selectedCount = selectedItems.length;

// Check for minimum requirements
if (selectedCount === 0) {
  alert('⚠️ No layers are selected!');
} else if (selectedCount < 2) {
  alert('⚠️ You need at least 2 color-filled layers selected!');
} else {
  // Create dialog for shape selection
  var dialog = new Window("dialog", "Select Shapes");
  dialog.orientation = "column";
  dialog.alignChildren = "left";

  // Foreground shape dropdown
  var fgGroup = dialog.add("group");
  fgGroup.add("statictext", undefined, "Foreground Shape:");
  var fgDropdown = fgGroup.add("dropdownlist", undefined, ["Square", "Circle"]);
  fgDropdown.selection = 1; // Default selection

  // Background shape dropdown
  var bgGroup = dialog.add("group");
  bgGroup.add("statictext", undefined, "Background Shape:");
  var bgDropdown = bgGroup.add("dropdownlist", undefined, ["Square", "Circle"]);
  bgDropdown.selection = 0; // Default selection

  // OK and Cancel buttons
  var buttonGroup = dialog.add("group");
  var cancelButton = buttonGroup.add("button", undefined, "Cancel");
  var okButton = buttonGroup.add("button", undefined, "OK");

  okButton.onClick = function () {
    var fgShape = fgDropdown.selection.text.toLowerCase();
    var bgShape = bgDropdown.selection.text.toLowerCase();

    // Get the size of the first selected object
    var backgroundSize = selectedItems[0].width;
    // Calculate foreground size (half of background size)
    var foregroundSize = backgroundSize / 2;

    // Function to create background shape
    function createForeground(y, x, size, fillColor) {
      if (fgShape === "square") {
        var square = doc.pathItems.rectangle(y - size / 2, x - size / 2, size, size);
        square.fillColor = fillColor;
        square.position = [x, y]; // Center foreground shape
        square.translate(-size / 2, -size / 2); // Align center
      } else if (fgShape === "circle") {
        var circle = doc.pathItems.ellipse(y, x, size, size);
        circle.fillColor = fillColor;
        circle.position = [x, y]; // Center foreground shape
        circle.translate(-size / 2, -size / 2); // Align center
      }
    }

    // Function to create foreground shape
    function createBackground(y, x, size, fillColor) {
      if (bgShape === "square") {
        var square = doc.pathItems.rectangle(y - size / 2, x - size / 2, size, size);
        square.fillColor = fillColor;
        square.position = [x, y + size / 2]; // Center background shape and shift down by half size
        square.translate(-size / 2, -size / 2); // Align center
      } else if (bgShape === "circle") {
        var circle = doc.pathItems.ellipse(y, x, size, size);
        circle.fillColor = fillColor;
        circle.position = [x, y + size / 2]; // Center background shape and shift down by half size
        circle.translate(-size / 2, -size / 2); // Align center
      }
    }

    // Initial sizes and positions with center alignment below the selected objects
    var swatchSpacing = selectedItems[1].left - selectedItems[0].left - backgroundSize;
    var artbX = selectedItems[0].left + selectedItems[0].width - backgroundSize / 2;
    var artbY = selectedItems[0].top + (selectedItems[0].height + swatchSpacing) * 2;

    // Create foreground and background shapes based on user choices
    for (var i = 0; i < selectedCount; i++) {
      var swatchY = artbY + (i * (backgroundSize + swatchSpacing));
      var colorRow = selectedItems[selectedCount - i - 1].fillColor;
      
      for (var j = 0; j < selectedCount; j++) {
        var swatchX = artbX + (j * (backgroundSize + swatchSpacing));
        var colorColumn = selectedItems[j].fillColor;

        createBackground(swatchY, swatchX, backgroundSize, colorRow);
        createForeground(swatchY, swatchX, foregroundSize, colorColumn);
      }
    }

    dialog.close();
    alert('Shapes created successfully!');
  };

  cancelButton.onClick = function () {
    dialog.close();
  };

  dialog.show();
}

date published

Aug 4, 2024

date published

Aug 4, 2024

date published

Aug 4, 2024

date published

Aug 4, 2024

reading time

5 min

reading time

5 min

reading time

5 min

reading time

5 min

.say hello

i'm open for freelance projects, feel free to email me to see how can we collaborate

.say hello

i'm open for freelance projects, feel free to email me to see how can we collaborate

.say hello

i'm open for freelance projects, feel free to email me to see how can we collaborate

.say hello

i'm open for freelance projects, feel free to email me to see how can we collaborate