Skip to content

Instantly share code, notes, and snippets.

@coerv
Created October 7, 2012 19:13
Show Gist options
  • Save coerv/3849281 to your computer and use it in GitHub Desktop.
Save coerv/3849281 to your computer and use it in GitHub Desktop.
Corvins Projektwochenzeug
main();
function main (){
/*** create document ***/
var phw = 200; // variable that holds the width/height of the page
var doc = app.documents.add();
doc.documentPreferences.pageWidth = phw;
doc.documentPreferences.pageHeight = phw;
doc.documentPreferences.facingPages = false;
var page = doc.pages.item(0);
/*** create swatches for the first textframe ***/
var colors1 = new Array(); // array that holds the swatches
for(var i=0; i<5; i++){
var my_color = doc.colors.add();
var R = Math.random() * 255;
var G = Math.random() * 255;
var B = Math.random() * 10;
my_color.name = "my new color"+ i;
my_color.model = ColorModel.PROCESS;
my_color.space = ColorSpace.RGB;
my_color.colorValue = [R,G,B];
// determine the brightness of the color
my_color_brightness = R + G + B;
colors1.push({"color": my_color,"name":my_color.name,"brightness":my_color_brightness});
}
alert(colors1.toSource());
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@fabiantheblind
Now lets sort these colors with our custom sort function below the main function
*/
colors1.sort(custom_compare);
//alert(colors1.toSource());
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@fabiantheblind
you can acess your color now via
you need to use an index or an loop
colors1[i].color ID Color object
colors1[i].name String
colors1[i].brightness Number
*/
/*** create textframe ***/
var topLeft = 0 + 10; // variable that holds the xy-coordinates of the upper-left corner of the textframe
var rightBottom = phw - 10; // variable that holds the xy-coordinates of the bottom-right corner of the textframe
// format text
var myfont = app.fonts.item("Delicious"+"\t"+"BoldItalic");
// create textframe
var tf = page.textFrames.add({
geometricBounds: [topLeft, topLeft, rightBottom, rightBottom],
contents: TextFrameContents.PLACEHOLDER_TEXT
});
var all_char = tf.characters.everyItem();
all_char.appliedFont = myfont;
all_char.leading = 8; // Textrahmen ist danach nur noch halb voll ????????????
//alert(myfont.name);
/*** Apply colour to the characters ***/
var chars = tf.characters; // all characters
for(var j=0; j < chars.length; j++){
for(var k=0; k < 5; k++){
//chars[j].fillColor = colors1[k].brightness; // ?????????????
}
}
}
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
this is deep Javascript Stuff
you can write your custom comparator
read this Stackoverflow post
http://stackoverflow.com/questions/1129216/sorting-objects-in-an-array-by-a-field-value-in-javascript
*/
function custom_compare(a,b) {
if (a.brightness < b.brightness)
return -1;
if (a.brightness > b.brightness)
return 1;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment