Adverti horiz upsell
How to sample color at any given point in a ramp
How to sample color at any given point in a ramp
tjgalda, updated 2008-04-08 17:22:50 UTC 24,060 views  Rating:
(3 ratings)
Page 1 of 1

From our website: www.tjgalda.com

Skill level: intermediate

How do you query the colour in a ramp at any given point?

Frustratingly, this isnt quite as simple as it sounds. Even the technical documentation from Autodesk states:

Currently the routines to get the value of a ramp structure (with interpolation) are not available through MEL, which limits the use of this control by end users.

So, we have to work around it in this case. We are able to sample the ramp and get back i)how many colour entries there are, ii)where they are in the ramp and iii) what colour they are. From that data, we can approximate fairly well what the ramp looks like. Lets build the tool for our example to do so on a relatively simple scale. You can add complexity later, but to keep things simple, were going to sample a simple ramp with two colours and a linear interpolation between the two colours. A ramp such as this mel code will create:

Note: you can view all the mel on the website. www.tjgalda.com


Create the gradient using a ramp:
string $tempName = `shadingNode -asShader ramp`;
string $tj_rampNode = `rename $tempName tj_gradientColour`;
string $tj_rampWin = `window -t "tj Ramp Viewer" tj_rampWin`;
columnLayout;
rampColorPort -node $tj_rampNode;
showWindow $tj_rampWin;

setAttr ($tj_rampNode + ".colorEntryList[0].color") -type double3 0.988 1 0.8 ;
setAttr ($tj_rampNode + ".colorEntryList[1].color") -type double3 0 0 0.235 ;
setAttr ($tj_rampNode + ".colorEntryList[1].position") 1.0;
removeMultiInstance -break true ($tj_rampNode + ".colorEntryList[2]");
showWindow $tj_rampWin;



Now that we have the ramp, we can gather information about the ramp. We need to determine where are they and what colour, create an array to catch each position and match the entry in the list of the ramp. Finally we will create an array to catch colour, use a vector array to maintain match in the list of the ramp.

Where are they and what colour? Create an array to catch each position and match entry in list.
int $entries = `getAttr -size ($tj_rampNode+".colorEntryList")`;
vector $entry_colour[];
float $entry_pos[];
for ($i = 0; $i < $entries; $i ++)
{
$entry_pos[$i] = `getAttr ($tj_rampNode+".colorEntryList["+$i+"].position")`;
float $entryList_colours[] = `getAttr ($tj_rampNode+".colorEntryList["+$i+"].color")`;
$entry_colour[$i] = << $entryList_colours[0],$entryList_colours[1], $entryList_colours[2]>>;
}

//and print back the results
print ("\n$entry_pos[] is \n==================\n");
print $entry_pos;
print ("\n\n==================\n$entry_colour[] is \n==================\n");
print $entry_colour;



$entry_pos[] is
==================
0
1
==================
$entry_colour[] is
==================
0.988 1 0.8
0 0 0.235


In our example now, we can see that we have the two colour entries at 0 and 1, with the associated colour mix. Now we can work with some basic calculations to deduce what the colours are between. After we know the colour information, we can then begin to deduce what the ramp is like. The first step is to find the difference or delta between the two colours on each channel. From there, we can divide up the colour on regular intervals and determine what the exact colour is at any specific point on the map.

This is a sampling step. How many intervals should we break the ramp into?
int $step = 20;



Find the delta/difference for each channel. Use temp vectors to get data out of the array easily. For our example to keep it simple, we can make some assumptions about where each entry is and what colours they are. We will assume only two colours, and that the first colour is brighter. Of course, one should build in robustness to remove these assumptions with true data.

vector $tempTopV = $entry_colour[0];
vector $tempBotV = $entry_colour[1];

//Use absolute to ensure its not a negative value
float $differenceR = `abs($tempTopV.x - $tempBotV.x)`;
float $differenceG = `abs($tempTopV.y - $tempBotV.y)`;
float $differenceB = `abs($tempTopV.z - $tempBotV.z)`;



Divide up the delta per step. This is the amount of change per step that each colour travels.
float $deltaStepR = $differenceR / $step;
float $deltaStepG = $differenceG / $step;
float $deltaStepB = $differenceB / $step;



Now, start number + (delta per step * the number of steps) = end number, in other words, we have broken down what the difference can be at each interval. Finalize by building the arrays.
float $colourStepsR[], $colourStepsG[], $colourStepsB[];
$colourStepsR[0] = $tempBotV.x;
$colourStepsG[0] = $tempBotV.y;
$colourStepsB[0] = $tempBotV.z;



Each new step is the old step + the delta for that colour.
for ($i = 1; $i <= $step; $i++)
{
$colourStepsR[$i] = $colourStepsR[$i - 1] + $deltaStepR;
$colourStepsG[$i] = $colourStepsG[$i - 1] + $deltaStepG;
$colourStepsB[$i] = $colourStepsB[$i - 1] + $deltaStepB;
}

print ("\n===================\n Colour intervals for R(with a step of" +$deltaStepR+") is:\n");
print $colourStepsR;
print ("\n===================\n Colour intervals for G(with a step of" +$deltaStepG+") is:\n");
print $colourStepsG;
print ("\n===================\n Colour intervals for B(with a step of" +$deltaStepB+") is:\n");
print $colourStepsB;




The results for our simple example are printed below. Of course, from here the complexity only increases. One needs to take in to account the type of interpolation, and the number of entries, but the procedure of sampling will all remain the same as outlined above.

As you can see, its a lot of legwork for looking up a simple colour attribute. :/

-tj July 2006

Note: you can view all the mel on the website. www.tjgalda.com



===================
Colour intervals for R (with a step of 0.04939999878) is:
0
0.0494
0.0988
0.1482
0.1976
0.247
0.2964
0.3458
0.3952
0.4446
0.494
0.5434
0.5928
0.6422
0.6916
0.741
0.7904
0.8398
0.8892
0.9386
0.988
===================
Colour intervals for G (with a step of 0.05) is:
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0.55
0.6
0.65
0.7
0.75
0.8
0.85
0.9
0.95
1
===================
Colour intervals for B (with a step of 0.02825000063) is:
0.235
0.26325
0.2915
0.31975
0.348
0.37625
0.4045
0.43275
0.461
0.48925
0.5175
0.54575
0.574
0.60225




Note: you can view all the mel on the website. www.tjgalda.com

If you like this tutorial, or have ideas for other topics, let me know.

tj




Comments

  • Joojaa

    Joojaa said almost 8 years ago:

    All of whats on this page is complete and utter tosh. Simply a programmer that doenst actually know how things relate to another. So yes even autodesk guys are sort of wrong here*. And yes thsi would have worked even in Maya 1.0 See the thing is MEL is not a programming language per see. Not supprisingly DG does know how to evaluate the ramp, this by the way is the trick to using maya and mel. Lets take the ramp in this scene. So how do we query the values then? Well simple we use setAttr and getAttr. See maya shading nodes work like follows they gather input in one end and push output in the other. So its rather simple to do the listing: for ($i=0.0;$i
  • evadceivorus

    evadceivorus said over 6 years ago:

    Joojaa's example doesn't quite do it. The out color will just print the overall color when queried. Mel has a command to get such a thing. colorAtPoint -output RGB -u .5 -v .1 "ramp1"

Please sign up or sign in to post a comment.