Sg2.9
modify the HSB values of a co...
Recent RSS
modify the HSB values of a color?
From:  mikima
Date:  18. October 2010, 23:04

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

Re: modify the HSB values of a color?
From:  clankill3r
Date:  12. November 2010, 11:33

my idea is that it is not possible:

http://scriptographer.org/reference/ai/color/

seems like hsb is not supported

Re: modify the HSB values of a color?
From:  mikima
Date:  8. December 2010, 11:21

i don't find any reference for hsb color in the page you linked. maybe i miss something?

Re: modify the HSB values of a color?
From:  Jürg Lehni
Date:  10. December 2010, 18:50

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);
		}
	}
}

Re: modify the HSB values of a color?
From:  doeke wartena
Date:  23. January 2011, 14:35

Hi Jürg,

i try to print images with different colors then cmyk or rgb.
For this i want to try to understand the proces of rgb to cmyk.
Do you know something simulair like the thing above?

Re: modify the HSB values of a color?
From:  Jürg Lehni
Date:  23. January 2011, 14:45

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!

Re: modify the HSB values of a color?
From:  doeke wartena
Date:  23. January 2011, 14:55

[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.

Re: modify the HSB values of a color?
From:  Jürg Lehni
Date:  23. January 2011, 23:28

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.

Re: modify the HSB values of a color?
Date:  24. January 2011, 12:26

Whenever you set a color to an item in your document, that color will be converted to the document's color space

Re: modify the HSB values of a color?
From:  doeke wartena
Date:  26. January 2011, 14:27

Thanks, this was very helpfull

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Stroke Input 09.03.09