 # vertexcolor range [SOLVED]

Hi,
i’m pretty stuck with a vertex color range problem.
I’ve got a floating value between 0 and 1 which i associate with vertex of a plane

how could i make this value a color range for vertex color in rgb?

Actually accessing the r,g, and b settings of a vertex on a mesh face is okay for me, but converting those floating values into a range for each r,g,b between 0 to 255 is still hard to understand.

rgbLevel = int(round(255 * yourValue))

thanks macouno

for a minimum value = 0 (r=0,g=0,b=255)
for a maximum value = 1(r=255,g=0,b=0)

what i don’t understand is more how to set r, g and b between those two limits ?

There isn’t a single way to accomplish what you’re trying to do, so it depends on what’s your color repartition on [0:1].
This is an example code I made in Javascript some time ago:

``````
if(x &lt;= 43){
colorR = 255;
colorG = x * 6;
colorG = colorG + Math.floor((255 - colorG) * y / 255);
colorB = y;
}
else if(x &gt; 43 && x &lt;= 86){
colorR = 255 - ((x - 43) * 6);
colorR = colorR + Math.floor((255 - colorR) * y / 255);
colorG = 255;
colorB = y;
}
else if(x &gt; 86 && x &lt;= 128){
colorR = y;
colorG = 255;
colorB = (x - 86) * 6;
colorB = colorB + Math.floor((255 - colorB) * y / 255);
}
else if(x &gt; 128 && x &lt;= 170){
colorR = y;
colorG = 255 - ((x - 128) * 6);
colorG = colorG + Math.floor((255 - colorG) * y / 255);
colorB = 255;
}
else if(x &gt; 170 && x &lt;= 213){
colorR = (x - 170) * 6;
colorR = colorR + Math.floor((255 - colorR) * y / 255);
colorG = y;
colorB = 255;
}
else if(x &gt; 213 && x &lt;= 256){
colorR = 255;
colorG = y;
colorB = 255 - ((x - 213) * 6);
colorB = colorB + Math.floor((255 - colorB) * y / 255);
}
``````

It matchs this colorband (x=0 on the left and x=1 on the right)
As you can see this colorband doens’t contain all possible colors, and I don’t think you’ll be easily able to do so (you’re trying to convert a single dimensional variable to a tri-dimensional one…). Plus, my repartition doesn’t match yours (you have blue on the left and red on the right).
But maybe you can took inspiration from this one.

Thanks matmdx it has been really helpful ``````

for face in ms.faces:

opt = 1

for i in xrange(len(face)):

points = face.v[i]
points.sel = 1

while opt == 1:

for elem in tamp:

if elem == points:

if elem == 0:

face.col[i].r = 0
face.col[i].g = 0
face.col[i].b = 255

elif elem == 0.5:

face.col[i].r = 0
face.col[i].g = 255
face.col[i].b = 0

elif elem &gt;= 1:

face.col[i].r = 255
face.col[i].g = 0
face.col[i].b = 0

elif 0 &lt; elem &lt; 0.5:

face.col[i].r = 0
face.col[i].g = int(elem*2*255)
face.col[i].b = 255 - int(elem*2*255)

elif 0.5 &lt; elem &lt; 1:

face.col[i].r = int((elem-0.5)*2*255)
face.col[i].g = 255 - int((elem-0.5)*2*255)
face.col[i].b = 0

opt = 0

``````

assuming elem is a floating value between 0 and 1
it works but there is still a problem when i display it in UV face select mode some corners of the faces are in white :eek::eek::eek:

I’will post a screen shot in the next reply

Grr… it seems that i’m not allowed to add attachments so here is a link for the screenshot

PROBLEM SOLVED

``````

for face in ms.faces:

for i in xrange(len(face)):

points = face.v[i]
points.sel = 1

for elem in tamp:

if elem == points:

if elem == 0:

face.col[i].r = 0
face.col[i].g = 0
face.col[i].b = 255

elif elem == 0.5:

face.col[i].r = 0
face.col[i].g = 255
face.col[i].b = 0

elif elem &gt;= 1:

face.col[i].r = 255
face.col[i].g = 0
face.col[i].b = 0

elif 0 &lt; elem &lt; 0.5:

face.col[i].r = 0
face.col[i].g = int(elem*2*255)
face.col[i].b = 255 - int(elem*2*255)

elif 0.5 &lt; elem &lt; 1:

face.col[i].r = int((elem-0.5)*2*255)
face.col[i].g = 255 - int((elem-0.5)*2*255)
face.col[i].b = 0

ms.update()

``````