def test_rgb_to_hsv(): """ Test translation function rgb_to_hsv """ rgb = introcs.RGB(255, 255, 255) hsv = a2.rgb_to_hsv(rgb) introcs.assert_equals('0.000', a2.str5(hsv.hue)) introcs.assert_equals('0.000', a2.str5(hsv.saturation)) introcs.assert_equals('1.000', a2.str5(hsv.value)) rgb = introcs.RGB(62, 7, 7) hsv = a2.rgb_to_hsv(rgb) introcs.assert_equals('0.000', a2.str5(hsv.hue)) introcs.assert_equals('0.887', a2.str5(hsv.saturation)) introcs.assert_equals('0.243', a2.str5(hsv.value)) rgb = introcs.RGB(188, 56, 9) hsv = a2.rgb_to_hsv(rgb) introcs.assert_equals('15.75', a2.str5(hsv.hue)) introcs.assert_equals('0.952', a2.str5(hsv.saturation)) introcs.assert_equals('0.737', a2.str5(hsv.value)) rgb = introcs.RGB(6, 2, 8) hsv = a2.rgb_to_hsv(rgb) introcs.assert_equals('280.0', a2.str5(hsv.hue)) introcs.assert_equals('0.750', a2.str5(hsv.saturation)) introcs.assert_equals('0.031', a2.str5(hsv.value)) rgb = introcs.RGB(24, 178, 88) hsv = a2.rgb_to_hsv(rgb) introcs.assert_equals('144.9', a2.str5(hsv.hue)) introcs.assert_equals('0.865', a2.str5(hsv.saturation)) introcs.assert_equals('0.698', a2.str5(hsv.value))
def on_rgb_press(self,r,g,b): """ Callback to rgb button """ self.rgb = introcs.RGB(r, g, b) self.hsv = a2.rgb_to_hsv(self.rgb) assert (self.hsv == None or type(self.hsv) == introcs.HSV), \ 'rgb_to_hsv does not return a HSV object' self.cmyk = a2.rgb_to_cmyk(self.rgb) assert (self.cmyk == None or type(self.cmyk) == introcs.CMYK), \ 'rgb_to_cmyk does not return a CMYK object' self.update()
def on_cmyk_press(self,c,m,y,k): """ Callback to cmyk button """ self.cmyk = introcs.CMYK(c, m, y, k) temp = a2.cmyk_to_rgb(self.cmyk) assert (temp == None or type(temp) == introcs.RGB), \ 'cmyk_to_rgb does not return a RGB object' self.rgb = self.rgb if temp is None else temp self.hsv = a2.rgb_to_hsv(self.rgb) assert (self.hsv == None or type(self.hsv) == introcs.HSV), \ 'rgb_to_hsv does not return a HSV object' self.update()
def register(self): """ Initializes color values and forces refresh """ active = True self.rgb = introcs.RGB(0, 255, 0) self.cmyk = a2.rgb_to_cmyk(self.rgb) assert (self.cmyk == None or type(self.cmyk) == introcs.CMYK), \ 'rgb_to_cmyk does not return a CMYK object' self.hsv = a2.rgb_to_hsv(self.rgb) assert (self.hsv == None or type(self.hsv) == introcs.HSV), \ 'rgb_to_hsv does not return a HSV object' self.top.register() self.update()
def on_rgb_slide(self,r,g,b): """ Callback to rgb sliders """ if not self.active: return red = int(round(r / 100.0)) green = int(round(g / 100.0)) blue = int(round(b / 100.0)) self.rgb = introcs.RGB(red, green, blue) self.hsv = a2.rgb_to_hsv(self.rgb) assert (self.hsv == None or type(self.hsv) == introcs.HSV), \ 'rgb_to_hsv does not return a HSV object' self.cmyk = a2.rgb_to_cmyk(self.rgb) assert (self.cmyk == None or type(self.cmyk) == introcs.CMYK), \ 'rgb_to_cmyk does not return a CMYK object' self.update()
def on_cmyk_slide(self,c,m,y,k): """ Callback to cmyk sliders """ if not self.active: return cyan = c / 100.0 magenta = m / 100.0 yellow = y / 100.0 black = k / 100.0 self.cmyk = introcs.CMYK(cyan, magenta, yellow, black) temp = a2.cmyk_to_rgb(self.cmyk) assert (temp == None or type(temp) == introcs.RGB),\ 'cmyk_to_rgb does not return a RGB object' self.rgb = self.rgb if temp is None else temp self.hsv = a2.rgb_to_hsv(self.rgb) assert (self.hsv == None or type(self.hsv) == introcs.HSV), \ 'rgb_to_hsv does not return a HSV object' self.update()
def update(self, rgb, cmyk, hsv): """ Refreshes the color and text display in the color panels """ compRGB = a2.complement_rgb(rgb) if (compRGB is None): compRGB = rgb rgb_str = rgb_to_str(rgb) cmyk_str = '' if cmyk is None else str5_cmyk(cmyk) hsv_str = '' if hsv is None else str5_hsv(hsv) self.main.text = ("ORIGINAL COLOR\n\n"+ "Color\nRGB: " + rgb_str + "\nCMYK: " + cmyk_str + "\nHSV: " + hsv_str + "\n \n" + "R,G,B sliders in: 0..255\n" + "C,M,Y,K sliders: 0 to 100%\n" + "H slider: 0 <= H < 360 degrees\n" + "S,V sliders: 0 <= S,V <= 1") self.main.background = rgb.glColor() self.main.foreground = compRGB.glColor() if self.menu.text in self.menu.data: matrix = self.menu.data[self.menu.text] else: matrix = self.menu.data['Normal'] cbrgb = a2.apply_matrix(matrix,rgb) rgb_str = rgb_to_str(cbrgb) try: cmyk_str = str5_cmyk(a2.rgb_to_cmyk(cbrgb)) if cmyk_str is None: cmyk_str = '' except: cmyk_str = '' try: hsv_str = str5_hsv(a2.rgb_to_hsv(cbrgb)) if hsv_str is None: hsv_str = '' except: hsv_str = '' self.comp.text = ("TRANSFORMED COLOR\n\n"+ "Color\nRGB: " + rgb_str + "\nCMYK: " + cmyk_str + "\nHSV: " + hsv_str + "\n \n" + "R,G,B sliders in: 0..255\n" + "C,M,Y,K sliders: 0 to 100%\n" + "H slider: 0 <= H < 360 degrees\n" + "S,V sliders: 0 <= S,V <= 1" ) self.comp.background = cbrgb.glColor() self.comp.foreground = a2.apply_matrix(matrix,compRGB).glColor() # set the sliders self.rSlider.value = rgb.red*100 self.gSlider.value = rgb.green*100 self.bSlider.value = rgb.blue*100 self.cSlider.value = 0 if cmyk is None else cmyk.cyan*100 self.mSlider.value = 0 if cmyk is None else cmyk.magenta*100 self.ySlider.value = 0 if cmyk is None else cmyk.yellow*100 self.kSlider.value = 0 if cmyk is None else cmyk.black*100 self.hSlider.value = 0 if hsv is None else hsv.hue*100 self.sSlider.value = 0 if hsv is None else hsv.saturation*100 self.vSlider.value = 0 if hsv is None else hsv.value*100