Activity › Forums › Adobe After Effects Expressions › Link to another color and adjust it
-
Link to another color and adjust it
Posted by Nate Vander plas on June 14, 2010 at 8:17 pmI’m trying to figure out a way to link one color to another and then adjust that original color with an expression. Obviously it’s easy to link one color property to another color property with the pick whip, but I then want to adjust the hue (and maybe the lightness, saturation, and alpha) of that original color. For instance, if the original color is blue, I’d like to automatically transform the new color to something complimentary, like orange. I’ve seen some stuff on hslToRgb, but was unsuccessful with adapting that to my situation. I’m working in CS3, if that matters.
Thanks,
NateIvan Zornic replied 9 years ago 5 Members · 8 Replies -
8 Replies
-
Dan Ebberts
June 14, 2010 at 9:01 pmI’m not quite sure what you’re after, but this example would invert the color of the fill effect applied to another layer (named “master” in this example):
c = thisComp.layer(“master”).effect(“Fill”)(“Color”);
[1-c[0],1-c[1],1-c[2],c[3]]Dan
-
Filip Vandueren
June 15, 2010 at 1:02 amOr, if you just want to invert the hue:
c = thisComp.layer("master").effect("Fill")("Color");
hsl = rgbToHsl(c);
hue = (hsl[0]+0.5)%1;compl = [hue, hsl[1], hsl[2], hsl[3]];
hslToRgb(compl);
this will get the color that is 0.5 or 180° across on the color wheel, but with the same saturation, brightness and alpha as the master.
-
Stephen Botting
September 22, 2010 at 8:25 pmHi there,
I’m trying to do a similar thing but through extendscript, when I try to run it says that “Function hslToRgb is undefined”
Thanks,
Stephenvar inRgb = hslToRgb([0.5,0.5,0.5,0]); -
Dan Ebberts
September 22, 2010 at 8:32 pmScripting doesn’t have that function. I’ve seen the algorithm around somewhere — it shouldn’t be too hard to implement your own conversion function. Or, you could create a temporary layer, apply an expression that does the conversion, harvest the result, and delete the layer.
Dan
-
Stephen Botting
September 22, 2010 at 10:00 pmThanks Dan,
As you suggested I found a page with an algorithm to make the conversion.
When I try to run the script I wrote I get returned the error “| is undefined” with the line “if (S == 0) {” highlighted, if I comment out that “if” structure, I get the same error but on the “var rgb = new Array(3);”
Any suggestions?
Thanks,
StephenFor some reason all my < symbols are appearing as < tried to fix it and it changes back.
function hslToRGB(H,S,L) {
//https://130.116.54.154/~monger/hsl-rgb.htmlvar rgb = new Array(3);
if (S == 0) {
rgb[0] = L;
rgb[1] = L;
rgb[2] = L;
return rgb;
}if (L < 0.5) var temp2 = L * (1.0+S);
if (l >= 0.5) var temp2 = L+S-L*S;var temp1 = 2.0*L - temp2;
H = H/360;var temp3 = new Array(3);
temp3[0] = H+1.0/3.0;
temp3[1] = H;
temp3[2] = H-1.0/3.0;for (i=0; i < temp3.length; i++) {
if (temp3[i] < 0) temp3[i] = temp3[i] + 1.0;
if (temp3[i] > 1) temp3[i] = temp3[i] - 1.0;if (6.0 * temp3[i] < 1) {
rgb[i] = temp1 +(temp2-temp1) * 6.0 * temp3[i];
} else if (2.0*temp3[i] < 1) {
rgb[i] = temp2;
} else if (3.0*temp3[i] < 2) {
rgb[i] = temp1+(temp2-temp1)*((2.0/3.0)-temp3[i])*6.0;
} else {
rgb[i] = temp1;
}
}
return rgb;
}
-
Dan Ebberts
September 22, 2010 at 10:53 pmThis line is causing the problem:
if (l >= 0.5) var temp2 = L+S-L*S;
It should be
if (L >= 0.5) var temp2 = L+S-L*S;
Dan
-
Ivan Zornic
May 5, 2017 at 3:17 pmHi there,
I know that this tread is bit old but I couldn’t find inverse function: RgbToHsl().
I have created JS copy of function provided here:
https://wiki.secondlife.com/wiki/Color_conversion_scripts#RGB_to_HSL
but it returns some strange values.Any help will be appreciated.
function RgbToHsl(rgb){var r = rgb[0] / 255;
var g = rgb[1] / 255;
var b = rgb[2] / 255;var h;
var s;
var l;var max;
var min;// Looking for the max value among r, g and b
if (r > g && r > b) max= r;
else if (g > b) max = g;
else max = b;// Looking for the min value among r, g and b
if (r < g && r < b) min = r;
else if (g < b) min = g;
else min = b;l = (max + min) / 2;
if (max == min){
h = 0.0;
s = 0.0;
}else{var d = max - min;
if (l > 0.5) s = d / (2 - max - min);
else s = d / (max + min);if (max == r) {
if (g < b) h = (g - b) / d + 6;
else h = (g - b) / d;
}
else if (max == g)
h = (b - r) / d + 2;
else
h = (r - g) / d + 4;h /= 6;
}return [h, s, l];
}
Reply to this Discussion! Login or Sign Up