gamma-
gamma-

Reputation: 21

Simplest way of converting RGB to Saturation

I'm totally inexperienced with python or with any programming language, I'm a couple days into programming and I want to convert RGB values to saturation, I have no experience at all with programming, so keep the code simple, I'm using a program called Cinema 4D with python in a thing called "xpresso", I have plugged in the red green and blue values through "xpresso" to python so I don't need to define them by myself [here is link to screenshot of what i'm doing http://www.4shared.com/download/5UwPM-uEce/bandicam_2014-09-10_21-36-26-1.png?lgfp=3000 ], the only thing I need to do is to convert those values to saturation, this might be unclear question, or even wrong place to ask this question on but I'm going to ask it anyway, what would be the simplest way to convert RGB to saturation value with python

Upvotes: 1

Views: 212

Answers (2)

PawelP
PawelP

Reputation: 1180

If you want to calculate it "by hand":

def main():
    global Output
    Output = rgb_to_s(red, green, blue)

def rgb_to_s(r, g, b):
    # if you have 0-255 rgb values:
    r = float(r)/255
    g = float(g)/255
    b = float(b)/255

    cmax = max(r, g, b)
    cmin = min(r, g, b)
    l = (cmax + cmin)/2
    d = cmax - cmin

    return d/(1-abs(2*l - 1))

Upvotes: 1

nneonneo
nneonneo

Reputation: 179442

import colorsys
print colorsys.rgb_to_hls(r, g, b)[2]

Note that colorsys is a built-in module in Python. r, g and b should be specified as numbers between 0 and 1 (divide them by 255.0 if your numbers are between 0 and 255).

Upvotes: 3

Related Questions