土曜日, 8月 16, 2025

JavaScriptでAI遊び 61 
IllustratorでCMYK設定を指定%刻みに変更

前回のアップデー版です。
選択したオブジェクトのCMYK設定を指定%刻みに変更します。
ただしグループ化やグラデーション、パターンなどには対応しません。
 
ベースにしたデータを選択して実行・・・


それぞれのカラー設定


#target illustrator

function roundToIncrement(value, increment) {
return Math.round(value / increment) * increment;
}

function adjustCMYKToIncrement(increment) {
var doc = app.activeDocument;
var selection = doc.selection;

if (!selection.length) {
alert("オブジェクトを選択してください。");
return;
}

if (isNaN(increment) || increment <= 0 || increment > 100) {
alert("有効な%刻み(1-100)を入力してください。");
return;
}

for (var i = 0; i < selection.length; i++) {
var obj = selection[i];

if (obj.typename === "PathItem" && obj.filled) {
var color = obj.fillColor;
if (color.typename === "CMYKColor") {
color.cyan = roundToIncrement(color.cyan, increment);
color.magenta =
                    roundToIncrement(color.magenta, increment);
color.yellow = roundToIncrement(color.yellow, increment);
color.black = roundToIncrement(color.black, increment);
obj.fillColor = color;
}
}

if (obj.stroked) {
var strokeColor = obj.strokeColor;
if (strokeColor.typename === "CMYKColor") {
strokeColor.cyan =
                    roundToIncrement(strokeColor.cyan, increment);
strokeColor.magenta =
                    roundToIncrement(strokeColor.magenta, increment);
strokeColor.yellow =
                    roundToIncrement(strokeColor.yellow, increment);
strokeColor.black =
                    roundToIncrement(strokeColor.black, increment);
obj.strokeColor = strokeColor;
}
}
}

alert("CMYKカラーが " + increment +
        "% 刻みに四捨五入されました。");
}

// ユーザー入力を取得
var userInput =
    prompt("CMYKの刻み%を入力してください(例: 5, 10, 20)", "10");
if (userInput !== null) {
adjustCMYKToIncrement(parseFloat(userInput));
}

実行すると直ぐに上のメッセージが表示されるので指定地を入力します。

すると直ぐに上のメッセージが表示され・・・

カラー設定が変更されます。

処理後のカラー設定。