I want to modify the brightness of a color but i don't understand if is possible to do it and in which way.
Anyone have an idea?
thanks
mikima
Scriptographer currently does not support HSB natively, but this is quite easy to solve with a little script. The code below contains the two functions rgbToHsb() and hsbToRgb(). Called on a color, it returns an HSB object that contains the hue / saturation / brightness properties with values between 0 .. 360 for hue and 0 .. 100 for saturation and brightness.
The code at the beginning takes the fill color of the first currently selected item, prints it to the console, then converts it to HSB, prints that, and converts back for comparison.
I hope this helps!
var color = document.selectedItems[0].fillColor; print('Color', color); var hsb = rgbToHsb(color); print('Converted to HSB', Json.encode(hsb)); print('Converted back to RGB', hsbToRgb(hsb)); function rgbToHsb(color) { var r = color.red, g = color.green, b = color.blue; var max = Math.max(r, g, b), min = Math.min(r, g, b); var delta = max - min; var hue; var saturation = (max != 0) ? delta / max : 0; var brightness = max; if (saturation == 0) { hue = 0; } else { var rr = (max - r) / delta; var gr = (max - g) / delta; var br = (max - b) / delta; if (r == max) hue = br - gr; else if (g == max) hue = 2 + rr - br; else hue = 4 + gr - rr; hue /= 6; if (hue < 0) hue++; } return { hue: Math.round(hue * 360), saturation: Math.round(saturation * 100), brigthness: Math.round(brightness * 100) }; } function hsbToRgb(hsb) { var br = hsb.brigthness / 100; if (hsb.hue == 0) { return new RGBColor(br, br, br); } else { var h = hsb.hue % 360; var s = hsb.saturation, b = hsb.brigthness; var f = h % 60; var p = (b * (100 - s)) / 10000; var q = (b * (6000 - s * f)) / 600000; var t = (b * (6000 - s * (60 - f))) / 600000; switch (Math.floor(h / 60)) { case 0: return RGBColor(br, t, p); case 1: return RGBColor(q, br, p); case 2: return RGBColor(p, br, t); case 3: return RGBColor(p, q, br); case 4: return RGBColor(t, p, br); case 5: return RGBColor(br, p, q); } } }
Hi Doeke,
There is a similar conversion method for CMYK but it is very inaccurate. To get good results, you need take colour profiles into account. Luckily Scriptographer already handles these conversions for you. You can for exmpaple force a RGBColor object to behave like a CMYK one by simply access CMYK attributes on it (e.g. color.cyan). You can also explicitly convert it to one by using color.convert(type), e.g. color.convert('cmyk');.
I hope this helps!
[quote]There is a similar conversion method for CMYK but it is very inaccurate[/quote]
Could you show me?
And i saw the convert thing, i only don't understand it cause the color dialog always shows hsb rgb and cmyk at once and a document has a color setting. And if i print a rgb converted to cmyk then it still prints rgb values to the console so in what way is it converted?
I did not know about color.cyan, thanks for that.
How does it still print as RGB? Try this for example, it should really work:
print(new RGBColor(1, 0, 0).convert('cmyk'));
As for the other method, Google is your friend. A quick search brings up this: http://www.javascripter.net/faq/rgb2cmyk.htm. But since it is ignoring profiles, it really often is way off.