怎么把RGB转换成HSV?

时间:2019-07-10 09:03:02

标签: python colors converters

我必须将RGB转换为HSV,我已经获得了S和V的正确答案,但是H太高了。


class RGB(colour):


    def toHSV(self):


        var_R = self.values[0]
        var_G = self.values[1]
        var_B = self.values[2]


        cmax = max([var_R, var_G, var_B])
        cmin = min([var_R, var_G, var_B])
        dif = cmax - cmin
        V = cmax 


        if dif == 0:
            H = 0
        elif cmax == var_R:
            H = 60*(((var_G-var_B)/dif)%6)
        elif cmax == var_G:
            H = 60*(((var_B-var_R)/dif)+2)
        elif cmax == var_B:
            H = 60*(((var_R-var_G)/dif)+4)
        if cmax < 0:
            S = 0
            H = 0
        elif cmax != 0:
            S = (dif/cmax)
        if H < 0:
            H += 360
        return RGB([H, S, V])

col = RGB([0.9,0.8,0.6]) 打印(col.toHSV(),颜色([0.16666666666666666,0.25000000000000006,0.8]))

对于RGB R = 0.9,G = 0.8和B = 0.6,我必须得到答案0.17、0.25和0.8

但是对于H我得到0.4

0 个答案:

没有答案