-
Notifications
You must be signed in to change notification settings - Fork 30
HEX to RGB And Sort
fabiantheblind edited this page Apr 15, 2016
·
1 revision
#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";
// this sketch shows:
//
// transformation from hex to rgb
// array of objects sorting
// adding additional data to an object
/**
* Transform hex string to object
* @param {String} hexstr Hex string without #
* @return {Array} Array containing 3 values [RED, GREEN, BLUE]
*/
function rgbtohex(hexstr) {
return [
(parseInt(hexstr, 16) >> 16) & 0xff,
(parseInt(hexstr, 16) >> 8) & 0xff,
parseInt(hexstr, 16) & 0xff
];
}
/**
* calculates a sum of 3 values in an array
* @param {Array} rgb holds [RED, GREEN, BLUE]
* @return {Number} a sum of the RGB
*/
function combine(rgb) {
return rgb[0] + rgb[1] + rgb[2];
}
/**
* main function
*/
function draw() {
// our basic object
var obj = {
colors: [{
hex: '#123456'
}, {
hex: '#124567'
}, {
hex: '#537454'
}]
};
// take a look at it
b.println('basic json');
b.println(obj);
// data transformation
for (var i = 0; i < obj.colors.length; i++) {
var oneColor = obj.colors[i].hex; // isolate
var rgb = rgbtohex(oneColor.slice(1));// hex to rgb
obj.colors[i].rgb = rgb;// add to object
obj.colors[i].rgbsum = combine(rgb);// calc a sum (brightest to darkest)
}
// now sort the array based on the sum
obj.colors.sort(function(a, b) {
var value1 = a.rgbsum;
var value2 = b.rgbsum;
if (value1 < value2) {
return -1;
}
if (value1 > value2) {
return 1;
}
return 0;
});
// take a look at the result
b.println('updated json');
b.println(obj);
var rectWidth = b.width / obj.colors.length;// the width for the rectangles
var x = 0;// x location. Will be updated in loop j
// loop
for (var j = 0; j < obj.colors.length; j++) {
b.fill(obj.colors[j].rgb[0], obj.colors[j].rgb[1], obj.colors[j].rgb[2]); // set fill color
b.rect(x, 0, rectWidth, b.height);// draw the rect
x += rectWidth; // update x
}
}// done
b.go(); // run it
This wiki is maintained by:
fabiantheblind
Thanks to:
- JohnDarnell for fixing lots of typos.
- jsp for fixing lots of typos.
- ltfschoen for fixing typos.
- wridgers for adding more links.
Thanks to the students from the seminar for asking all those questions and making me start this wiki.
- adinaradke
- AnitaMei
- ce0311
- coerv
- felixharle
- FerdinandP
- Flave
- marche
- monkian
- natael
- OliverMatelowski
- PDXIII
- praktischend
- schlompf
- skaim
You are awesome.
- Arrays
- Classes
- Comments
- Conditionals
- Functions
- Inspect Properties
- Loops
- Objects
- Output And Interaction
- Recursive Functions
- Inspect Properties
- Variables And Operations
- Extended JavaScript Guide
- Bridge Talk
- Create And Read Files
- ExtendScript Toolkit
- File
- Folder
- Includes JSX
- Object Watch
- Read In JSON From File And DONT Eval
- Storing Data In A Target Engine
- Target an application
- XML
- app
- Colorbrewer
- Colors And Swatches
- Delay And View
- Dialogs
- Documents
- Duplicate And Transform
- Event AfterSave
- Export IDML
- ExtendScript in InDesign Scripting DOM
- Fonts
- GeometricBounds and Coordinates
- Get named pageItems
- Graphic Lines
- Groups
- HSL Color Wheel
- Images
- Includes
- InsertionPoints
- Layers
- Line Feeds And Carrige Returns
- Masterspreads
- Matrix
- Objectstyles
- Outlines Groups Alignment
- Pages And Margins
- Pathfinder
- Placeholder Text
- Rectangles Ovals Polygons
- RulerOrigin
- Select words at insertionPoint
- Simple Find And Change Grep with FC Query
- Simple Find And Change Grep
- Simple Find And Change Text
- Spiro
- Styles
- Text Analysis ID FC
- Text Analysis
- Text Find Locations
- Text
- Transformation Matricies
- TransparencySettings
- XML creation and import