Deconstructing System.Drawing.Color in Python?

Hi,

I’m currently working on a Python script that deals with a lot of colours.
I have a couple of System.Drawing.Color objects, all in ARGB mode, that I want to deconstruct, in order to get their a, r, g and b values, preferably in a list.

I’m also looking for a way to dynamically get each colour components (i.e. a, r, g, b) maximum and minimum levels.

I couldn’t find a documentation for the System.Drawing library, so I’m kinda lost here.

Any ideas?

Thank you.

Maybe something like this could get you started?

# x input set to list access and type hint color

# list containing all the B values from list of RGBA colors
B = [i.B for i in x]

print "Minimum Value of B in Colors List is", min(B)
print "Maximum Value of B in Colors List is", max(B)
1 Like

Hi @diff-arch, does this help?

1 Like

Thanks @Dancergraham and @chanley!

If you’re interested in a Python method that lerps two colours, in order to find a third one inbetween, check this out:

def lerp(start, stop, amt):
    """Calculates a number between two numbers at a specific increment.
    
    The amt parameter is the amount to interpolate between the two values,
    where 0.0 is equal to the first point, 0.1 is very near the first point,
    0.5 is half-way in between, and 1.0 is equal to the second point.
    If the value of amt is more than 1.0 or less than 0.0, the number will be
    calculated accordingly in the ratio of the given numbers.
    
    Args:
      start: First value (number).
      stop: Second value (number).
      amt: Amount to interpolate (number).
    Returns:
      The lerped value.
    """
    return amt * (stop - start) + start


def lerp_color(c1, c2, amt):
    """Blends two colors to find a third color somewhere between them.
    
    Args:
      c1: ARGB Color to interpolate from.
      c2: ARGB Color to interpolate to.
      amt: Number between 0 and 1.
    Returns:
      The lerped ARGB Color.
    """
    amt = max(min(amt, 1), 0) # prevent extrapolation
    
    # Perfom interpolation
    l0 = lerp(c1.A, c2.A, amt)
    l1 = lerp(c1.R, c2.R, amt)
    l2 = lerp(c1.G, c2.G, amt)
    l3 = lerp(c1.B, c2.B, amt)
    
    return Color.FromArgb(l0, l1, l2, l3)
1 Like