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

sampleImage not finding accurate luma steps

New Here ,
Jun 02, 2025 Jun 02, 2025

I am trying to control the opacity of layers using the luma value of sample from beneath it in another comp, but it's not capturing the full spectrum from 0 (Black) to 1 (White).

My setup looks like this:

Main Comp - COIN_STYLE_DITHER
Control Comp - COIN (Contains a 10 step gradient from black to white)
40 x TILE_X Comps distributed across X, each with 10 x COLOR layers ('COLOR_X') that should turn on and off depending on what luma value is sampled from beneath it in the main comp.

My issue is it's combining steps of the gradient and only mapping from 0 - 0.443 Brightness.

I want every gradient band to a new colour appear over it so that 10 Colour layers map to 10 gradient bands.

I know sampleimage isn't supposed to be used in this way but I thought it would work, any help would be appreciated!

Screenshot 2025-06-02 at 13.49.45.pngScreenshot 2025-06-02 at 13.56.39.png

mainComp = comp("COIN_STYLE_DITHER");
tileName = thisComp.name; // e.g. TILE_01
mainTile = mainComp.layer(tileName);

// Convert center of tile to main comp space (assuming 50x50px size)
samplePos = mainTile.toComp([25, 25]);

// Sample the COIN layer at that position
coinLayer = mainComp.layer("COIN");
sample = coinLayer.sampleImage(samplePos, [1, 1], true, time);
lum = rgbToHsl(sample)[2]; // get lightness from HSL

// Determine color band from this layer's name
indexFromName = parseInt(thisLayer.name.split("_")[1]); // COLOR_1 → 1
steps = 10;
stepSize = 1 / steps;
minBrightness = (indexFromName - 1) * stepSize;
maxBrightness = indexFromName * stepSize;

// Only this layer turns on for its band
(lum >= minBrightness && lum < maxBrightness) ? 100 : 0;



TOPICS
Expressions
75
Translate
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 ,
Jun 03, 2025 Jun 03, 2025
LATEST

Hi,

 

I've tried out your project and I can see the logic, but it feels very complicated.  I tried a different method regarding where I planned to set the colour of the solid via a fill effect and an array of colours.  It means significantly fewer expressions running too.  

One issue I ran into is that you have 9 colours, but 10 bands of grey.  

This is my code:

var solid = thisComp.layer("Colour Solid");
var target = thisComp.layer("COIN");


var rgb = target.sampleImage(solid.transform.position,[5,5],true,time);
var lum = rgbToHsl(rgb)[2];


var coloursArray = ['FF0000','FFB300','FFF300','5EFF00','00FFD8','0079FF','3300FF','FB00FF','FF007B'];
var roundLum = Math.floor(lum*10);
if (roundLum==10) {
	roundLum = 8;
}

hexToRgb(coloursArray[roundLum]);

Math.floor produced a range of numbers from 0 to 8, but in the white band, the final number was 10, as it was the only issue, I just added an IF statement to drop the number to 9 - which is when I spotted the different number of bands.  If you reduce the gray bands to 9 or add an additional colour in the array, then you can alter the roundLum to 8.

 

I've attached a copy of your project with my code.  

 

Translate
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