Page MenuHome

Add optional Alpha channel to Python class Color
Needs RevisionPublic

Authored by Gaia Clary (gaiaclary) on Apr 23 2018, 6:19 PM.

Details

Summary

Previous versions of Blender used the class Color
for Mesh vertex_colors. However Color has only 3
values (rgb). But vertex colors now also have an
Alpha channel. This patch is an attempt to add the
Alpha channel as optional 4th parameter to the
Color class. I know it is not completed, but maybe
i can get some hints what is missing or needs to be
changed.

Diff Detail

Repository
rB Blender
Branch
color
Build Status
Buildable 1447
Build 1447: arc lint + arc unit

Event Timeline

Campbell Barton (campbellbarton) requested changes to this revision.Apr 24 2018, 10:48 AM

The problem with this patch is it assumed all color has alpha too.

This change needs to be done more carefully.

  • A color may have alpha or not (like a vector being 3D or 4D).
  • Mixing alpha with non-alpha should give errors (adding for eg).
  • A an alpha color could be accessed as a non-alpha using color.rgb for eg.
This revision now requires changes to proceed.Apr 24 2018, 10:48 AM

first to understand what exactly shall be done:

Is this the correct behavior ?

  • col can either be a Color((r,g,b)) or a Color((r,g,b,a))
  • col = Color() returns a new Color((r=0, g=0, b=0, a=1))
  • col = Color((0,0,0)) returns a new Color((r=0, g=0, b=0))
  • col = Color((0,0,0,0)) returns a new Color((r=0, g=0, b=0, a=0))
  • col = col.rgb() returns a new Color((r=0, g=0, b=0))
  • col = Color.rgb() returns a new Color((r=0, g=0, b=0))
  • col.has_alpha() returns True if col has 4 elements
  • col.a gives a tracedump when col has no alpha
  • Object.data.vertex_colors[].data is a list of Color((r,g,b,a)) items
  • Object.data.vertex_colors[n].data[m] is a Color((r,g,b,a))

Are the above assumptions all correct?
What else is needed on the Python side ?
Is there anything to be done in addition ?

One thing is: How do i make a Color with optional 4th component?
Is there an example code somewhere so i can learn what to do here?

@Gaia Clary (gaiaclary). this is much closer to how I think it should work.

Minor changes.

  • Color() shouldn't have alpha.
  • col.rgb can be an attribute, like vec.xyz. rgb Should not return new colors, it should return RGB components of the color instance.
  • has_alpha can be a readonly attribute. (we could also just check len(color) == 4).
  • operations between colors with/without alpha should be supported but error in the case rgb/rgba are mixed.
source/blender/python/mathutils/mathutils_Color.c
923–926

We should not assume colors have alpha, some input may be RGB data.

Suggest to have a size argument, as vector does.