Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

April 09, 2013

The over operator

For blending between two colors, here's a bit of mixing code:
# color_filters.py
filter_color = {'r': 0.9, 'g': 0.2, 'b': 0.1}
background_color = {'r': 0.9, 'g': 0.9, 'b': 0.4}
filter_alpha = 0.7
def over_operator(alpha, color1, color2):
color_out = {}
for component in color1.keys():
color_out[component] = \
(alpha * color1[component]) + ((1.0-alpha) * color2[component])
return color_out
new_color = over_operator(filter_alpha, filter_color, background_color)
for component in 'rgb':
print(component, new_color[component])
i tend to like this a little bit more, especially if I can reuse the color lambda
# color_filters.py
# full version https://gist.github.com/zeffii/5355384
col = lambda r, g, b : dict(zip('rgb', (r, g, b)))
filter_color = col(0.9, 0.2, 0.1)
background_color = col(0.9, 0.9, 0.4)
filter_alpha = 0.7
Or another lambda version https://gist.github.com/zeffii/5354106 abbreviated here:
# color_filters.py
col = lambda rgb : dict(zip('rgb', rgb))
filter_color = col((0.9, 0.2, 0.1))
background_color = col((0.9, 0.9, 0.4))
filter_alpha = 0.7
# rest is the same