• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Using Expressions to compare sampled Colors.

Engaged ,
May 07, 2021 May 07, 2021

Copy link to clipboard

Copied

In simple terms, I'm trying to compare a color that is pulled using the sampleImage() function, to a variable with a specific HEX value stored in it.

 

My scene has a composition in it that has several different colored "swatches" in the same position, separated by frames. So I can scrub the timeline and see if the changes to those colors will trigger the changes I need in the expression.

 

I'm using a series of if statements, and the if's always fail. Since AE has no variable "tracing", I'm using some text fields to display the value of these variables. I think I discovered why it's not working but it's completely unclear as to the cause.

 

I have a reference text field, displaying the value of this function:

hexToRgb("0x1B10DD");

The first value it displays reads:

0.10588235294117647

 

Then I have some basic test code on another text field layer:

var swatchComp = thisComp.layer("Color Swatches Pre-comp 1");

swatchComp.sampleImage([5,5],[1,1],false);

 

And its value reads:

0.10588236153125763

 

They should be the exact same value!

 

If this doesn't work, there's no way my if statements are going to work at all. Btw, I tried adjusting sampleImage to read the value before / after effects (e.g. false or true), no effect.

 

Appreciate any thoughts.

TOPICS
Error or problem , Expressions , Scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2021 May 07, 2021

Copy link to clipboard

Copied

If the problem is the time the value is sampled you have to throw in valueAtTime() so the expression has something to compare to. This isn't the exact code but it is the idea:

 

// possible If statement
if (sample.valueAtTime(time - .5) == sample.value){
   do this;
else {
   do that;
}

 

The idea is to look at the value at a half-second before the current time. Dan Ebberts could write that in his sleep, I need to open up AE and fiddle to make it work.

 

If you are comparing two values at the same time then you probably should convert the decimal float value to an 8-bit value so you have a better chance of eliminating microscopic errors in sampling the color. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 07, 2021 May 07, 2021

Copy link to clipboard

Copied

Appreciate the response Rick. Will look into the time offset.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 07, 2021 May 07, 2021

Copy link to clipboard

Copied

Agree with Rick. Consider rounding/ clipping the value or it may never match. And keep in mind that color management settings and footage interpretation matter, too.

 

Mylenium

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2021 May 08, 2021

Copy link to clipboard

Copied

I think the main issue is that if your reference color is in hex, the accuracy is only one part in 255, so all those digits in the decimal representation don't really mean much. So you probably want to keep your comparisons in the hex domain. If you do this:

Math.round(swatchComp.sampleImage([5,5],[.5,.5],false)[0]*255);

I'm guessing you'll get 27, which matches the 1B part (the red part) of your hex color.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 08, 2021 May 08, 2021

Copy link to clipboard

Copied

Thanks Dan - I snagged an RGB to HEX conversion use provided in another post. I'm SO close... it actually "works" but I'm getting a bad argument error, pretty much once I added that last line "hexToRgb". It changed the color of the text field correctly, but doesn't like something about the way I'm passing that value into the function. I'm sure it's because I'm using HEX / String values interchangeably, but again the lack of Trace Statements in AE makes it hard to see what data is being passed around.

 

It is a bit sloppy for ease of troubleshooting.  Is attached to the Fill > Color property of the text field.

 

Many thanks!!

 

 

function RGBtoHEX(theColor) {
	r = Math.round(theColor[0]*255).toString(16)
	if (r.length < 2) r = "0" + r;
	g = Math.round(theColor[1]*255).toString(16)
	if (g.length < 2) g = "0"	+ g;
	b = Math.round(theColor[2]*255).toString(16)
	if (b.length < 2) b = "0"	+ b;
	return (r+g+b).toUpperCase();
}


// Display color
var color1 = "000000";
var color2 = "FFFFFF";


// HEX Values of colors that will be sampled
var swatch1 = "17DD41";
var swatch2 = "1B10DD";
var swatch3 = "DDB85C";


// Figure out what display color to use based on the sampled color.
var swatchComp = thisComp.layer("Color Swatches Pre-comp 1");
var sampleColor = swatchComp.sampleImage([5,5],[1,1],true);

var s = RGBtoHEX(sampleColor);

var newColor;
if (s == swatch1) newColor=color1;
if (s == swatch2) newColor=color2;
if (s == swatch3) newColor=color1;

hexToRgb(newColor);

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2021 May 08, 2021

Copy link to clipboard

Copied

I think you need to set newColor to some default value in case none of your swatch tests match. I'm not sure that's what's wrong, but that's what I'd try first.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 08, 2021 May 08, 2021

Copy link to clipboard

Copied

LATEST

SOO, in my sloppiness that actually took care of the problem. SMH. It was triggering an initial error because it was sending a "null" value to the fill color. 

 

Thanks all!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines