Skip to main content
Participating Frequently
January 5, 2021
Question

'Random' color AI script with no adjacent colors the same

  • January 5, 2021
  • 1 reply
  • 1780 views

Could an Illustrator script like RandomSwatchesFill be modified to apply colors in such a way that no two colors from a group of swatches would be repeated in shapes that immediately border each other? I don't think this is truly random, but it's meant to make the distribution of colors feel more varied.

 

From what I've seen online, this would be an application of the four color theorem — or whatever number of colors are involved. For my own purposes, I would typically have more colors in a group of swatches — at least five or more.

 

Appreciate any insights and guidance!

This topic has been closed for replies.

1 reply

femkeblanco
Legend
January 6, 2021

Firstly, you'll need to define what "adjacent" means in terms of path items in Illustrator.  

midmodAuthor
Participating Frequently
January 10, 2021

By adjacent, I mean shapes that immediately border each other. In my case, the simplest example might be a checkerboard-like grid where colors are assigned so that no squares that border each other are colored the same. Thanks.

femkeblanco
Legend
March 22, 2021

This is a first attempt at implementing of a graph using an adjacency list:

 

You will need a list of "edges" to enter into a text box when running the script.  A graph is a group of vertices (or nodes), which in our case represent paths, connected to each other by edges (or links), which in our case represent shared borders.  An edge is defined by the two vertices it connects. 

 

In the picture below, the paths on the left are represented by the graph on the right.  Each line is an edge.  For example, path1 shares a border with path2 and path3.  This is represented by the two edges "path1, path2" and "path1, path3".  You will need a list of all these edges. 

 

 

This is a very frivolous implementation.  In fact, there's a bug I haven't fixed:  With more edges, there is a chance (about 1/10) that >4 colors will be needed. 

 

 

 

Test.ai file 

 

// instructions
// (1) name your paths
// (2) have a list of edges ready to input
// (3) an edge is a pair of path names separated by a comma
// (4) run in CMYK mode (or change the "color" part of the script to run in RGB mode)
// (5) select your paths and run

// input
var vertices = [];
for (var i = 0; i < selection.length;i++) {
    vertices.push(selection[i].name);
}
var test = 
"path1, path2\
path1, path3\
path2, path3\
path2, path4\
path2, path5\
path3, path5\
path3, path6\
path4, path5\
path5, path6";
var w = new Window ("dialog", "Edges");
var text1 = w.add ("edittext", [0, 0, 150, 140], "", {multiline: true, wantReturn: true});
text1.text = test;
w.add ("button", undefined, "OK");
w.show ();
var edges = text1.text.split("\n");
for (var i = 0; i < edges.length; i++) {
    edges[i] = edges[i].split(", ");
}
// create graph (adjacency list)
var list = {};
for (var i = 0; i < vertices.length; i++) {
    list[vertices[i]] = [];
}
for (var i = 0; i < edges.length; i++) {
    list[edges[i][0]].push(edges[i][1]);
    list[edges[i][1]].push(edges[i][0]);
}
// color
var paths =  app.activeDocument.pathItems;
var swatchez = app.activeDocument.swatches;
var c = swatchez["CMYK Cyan"].color,
m = swatchez["CMYK Magenta"].color,
y = swatchez["CMYK Yellow"].color,
k = swatchez["Black"].color;
for (var key in list) {
    var colorz = [c, m, y, k];
    for (var i = 0; i < list[key].length; i++) {
        for (var l = colorz.length - 1; l > -1 ; l--) {
            var c1 = colorz[l];
            var c2 = paths[list[key][i]].fillColor;
            if (c1.cyan == c2.cyan && c1.magenta == c2.magenta && 
                c1.yellow == c2.yellow && c1.black == c2.black) {
                colorz.splice(l, 1);
            }
        }
    }
    var index = Math.floor(Math.random() * colorz.length);
    if (!colorz[index]) {
        paths[key].fillColor = swatchez["White"].color;
    } else {
        paths[key].fillColor = colorz[index];
    }
}