Skip to content

Instantly share code, notes, and snippets.

@majikcroaks
Forked from akaleeroy/Flatten Black.md
Created October 17, 2021 04:54
Show Gist options
  • Save majikcroaks/eb10bc31fe0a11a7c9c020374c8e1e35 to your computer and use it in GitHub Desktop.
Save majikcroaks/eb10bc31fe0a11a7c9c020374c8e1e35 to your computer and use it in GitHub Desktop.
Flatten Black - Illustrator script to batch convert rich blacks to 100%K

Flatten Black

Convert rich blacks to flat black

Flatten Black.jsx Demo

Description

Finds all items with enhanced blacks in the artwork and changes their color to flat black.

It has no prompts because it should work inside a Batch action.

Usage

Save the file and drop it onto the artboard. Or copy it to Illustrator's Scripts folder and launch it from File > Scripts

Default settings are to flatten any color with more than 90%K and 300% coverage. To change this limit open the .jsx file in a text editor and change var maxBlack = 95 and var minCoverage = 300 to your desired values.

You can add this to an action by using Insert Menu Item. Then save your action to an .aia file ( ▾☰ > Save Actions... ).

TODOs

  • Fix Target layer cannot be modified error – locked layers support
#target Illustrator
/* Convert rich blacks to flat black
* Finds items with enhanced blacks in the artwork and changes their color to flat black
*
* No prompts because it should work inside a Batch action!
* TODO: Error Target layer cannot be modified when another layer exists and is locked or smth
*/
// Black limit
// All colors with more than this amount of K and coverage will be flattened
// EDIT HERE
var maxBlack = 90;
var minCoverage = 300;
(function init() {
if(documents.length < 1) {
return;
} else {
main();
}
})();
function main() {
var docRef = app.activeDocument;
with (docRef) {
// Stop if document isn't CMYK
if(documentColorSpace !== DocumentColorSpace.CMYK) {
alert("Document isn't CMYK.");
return;
}
// Extend CMYKColor object with coverage calculation method
CMYKColor.getCoverage = function(color) {
return color.black + color.cyan + color.magenta + color.yellow;
};
// Create flat black Color
flatBlack = new CMYKColor();
flatBlack.black = 100;
// Iterate through all path items
for (var i = 0; i < pathItems.length; i++) {
with (pathItems[i]) {
if (filled == true && fillColor instanceof CMYKColor) {
// If black exceeds maxBlack clip it to flat black (100%K)
if (fillColor.black >= maxBlack && CMYKColor.getCoverage(fillColor) >= minCoverage) fillColor = flatBlack;
}
if (stroked == true && strokeColor instanceof CMYKColor) {
if (strokeColor.black >= maxBlack && CMYKColor.getCoverage(fillColor) >= minCoverage) strokeColor = flatBlack;
}
if (filled == true && fillColor instanceof SpotColor) {
if (fillColor.spot.color.black >= maxBlack && CMYKColor.getCoverage(fillColor) >= minCoverage) fillColor.spot.color = flatBlack;
}
if (stroked == true && strokeColor instanceof SpotColor) {
if (strokeColor.spot.color.black >= maxBlack && CMYKColor.getCoverage(fillColor) >= minCoverage) strokeColor.spot.color = flatBlack;
}
}
}
// Iterate through text items as well
for (var j = 0; j < stories.length; j++) {
with (stories[j]) {
for (var k = 0; k < characters.length; k++) {
with (characters[k].characterAttributes) {
if (fillColor instanceof CMYKColor) {
if (fillColor.black >= maxBlack && CMYKColor.getCoverage(fillColor) >= minCoverage) fillColor = flatBlack;
}
if (strokeColor instanceof CMYKColor) {
if (strokeColor.black >= maxBlack && CMYKColor.getCoverage(fillColor) >= minCoverage) strokeColor = flatBlack;
}
if (fillColor instanceof SpotColor) {
if (fillColor.spot.color.black >= maxBlack && CMYKColor.getCoverage(fillColor) >= minCoverage) fillColor.spot.color = flatBlack;
}
if (strokeColor instanceof SpotColor) {
if (strokeColor.spot.color.black >= maxBlack && CMYKColor.getCoverage(fillColor) >= minCoverage) strokeColor.spot.color = flatBlack;
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment