Can treat Color values akin to Vector.
The following example, subtracts two colors, normalized to [0, 1], from each other. The result is converted to a vector. If the length of the resultant vector is less than some tolerance then the colors "match".
import bpy
from mathutils import Vector, Color
def color_match(col1, col2, tol=0.001):
'''
Return true if vector col1 is within tol of vector col2
'''
def vector(col):
# sanitize range (-2, 3, 5) = (0, 1, 1)
return Vector([max(0, min(1, c)) for c in col])
d = vector(col1) - vector(col2)
return d.length <= tol
# test code
white = Color((1, 1, 1))
# comparing instances
Color((1, 1, 0.999999)) == white
# False
# same again using color_match no tol
color_match(Color((1, 1, 0.999999)), white, tol=0)
# False
# with default tol
color_match(Color((1, 1, 0.999)), white)
# True
color_match(Color((1, 1, 0.995)), white)
# False
# shorthand
color_match((1, 1, 1), white)
# True
Suggest always use some tolerance test (fabs(x - y) < tol
)
when Comparing Floating Point Values
. An example in test code above is
Color((1, 1, 0.999999))
which is NOT equal to
Color((1, 1, 1))
when it may be more desirable that they are treated as equal (or close enough).
print
actually return an array of(1,1,1
)? Also note that the documentation states that the values can be in the range [0,inf]. And floating point numbers have numerical inaccuracies, so comparison for equality is usually problematic. You could try something along the lines ofif(mat2.diffuse_color >= (0.9,0.9,0.9)):
$\endgroup$r
) ranging from 0 to 2.5 can set with withmax(r, 1)
$\endgroup$