Esempio n. 1
0
def rgb_to_cmyk(rgb):
    """
    Returns: color rgb in space CMYK, with the most black possible.

    Formulae from en.wikipedia.org/wiki/CMYK_color_model.

    Parameter rgb: the color to convert to a CMYK object
    Precondition: rgb is an RGB object
    """
    # The RGB numbers are in the range 0..255.
    # Change the RGB numbers to the range 0..1 by dividing them by 255.0.
    
    R = (rgb.red/255.0)
    G = (rgb.green/255.0)
    B = (rgb.blue/255.0)
    Cyan = 1 - R
    Magenta = 1 - G
    Yellow = 1 - B
    if Cyan == 1 and Magenta == 1 and Yellow == 1:
        return introcs.CMYK(0.0,0.0,0.0,100.0)
    else:
        K = min(Cyan,Magenta,Yellow)
        C = ((Cyan-K)/(1-K))*100.0
        M = ((Magenta-K)/(1-K))*100.0
        Y = ((Yellow-K)/(1-K))*100.0
        return introcs.CMYK(C,M,Y,K*100.0)
Esempio n. 2
0
def test_str5_color():
    """
    Test the str5 functions for cmyk and hsv.
    """
    print('Testing str5_cmyk and str5_hsv')
    # Tests for str5_cmyk
    # We need to make sure the coordinates round properly
    text = a3.str5_cmyk(introcs.CMYK(98.448, 25.362, 72.8, 1.0))
    introcs.assert_equals('(98.45, 25.36, 72.80, 1.000)',text)
    #another test
    text = a3.str5_cmyk(introcs.CMYK(0.0, 1.5273, 100.0, 57.846))
    introcs.assert_equals('(0.000, 1.527, 100.0, 57.85)',text)
    # Tests for str5_hsv (add two)
    text = a3.str5_hsv(introcs.HSV(98.448, 0.123456789, 0.0))
    introcs.assert_equals('(98.45, 0.123, 0.000)',text)

    text = a3.str5_hsv(introcs.HSV(0.0,0.313725490196,1.0))
    introcs.assert_equals('(0.000, 0.314, 1.000)',text)
    #test max value for H
    text = a3.str5_hsv(introcs.HSV(359.99,1.0,1.0))
    introcs.assert_equals('(360.0, 1.000, 1.000)',text)
    #test min values
    text = a3.str5_hsv(introcs.HSV(0.0,0.0,0.0))
    introcs.assert_equals('(0.000, 0.000, 0.000)',text)
    print('Tests for str5_cmyk and str5_hsv passed')
Esempio n. 3
0
def rgb_to_cmyk(rgb):
    """
    Returns a CMYK object equivalent to rgb, with the most black possible.

    Formulae from https://www.rapidtables.com/convert/color/rgb-to-cmyk.html

    Parameter rgb: the color to convert to a CMYK object
    Precondition: rgb is an RGB object
    """
    assert (type(rgb) == introcs.RGB), rgb + ' is not a RGB object'
    # The RGB numbers are in the range 0..255.
    # Change them to the range 0..1 by dividing them by 255.0.
    red = rgb.red / 255.0
    green = rgb.green / 255.0
    blue = rgb.blue / 255.0
    black = 1 - max(red, green, blue)
    if (black == 1):
        cyan = 0.0
        magenta = 0.0
        yellow = 0.0
    else:
        cyan = (1 - red - black) / (1 - black) * 100.0
        magenta = (1 - green - black) / (1 - black) * 100.0
        yellow = (1 - blue - black) / (1 - black) * 100.0
    black *= 100.0
    return introcs.CMYK(cyan, magenta, yellow, black)
Esempio n. 4
0
def test_str5_color():
    """
    Test the str5 functions for cmyk and hsv.
    """
    introcs.assert_equals(
        '(98.45, 25.36, 72.80, 1.000)',
        a2.str5_cmyk(introcs.CMYK(98.448, 25.362, 72.8, 1.0)))
    introcs.assert_equals(
        '(76.86, 0.000, 9.429, 100.0)',
        a2.str5_cmyk(introcs.CMYK(76.858, 0.000, 9.42885, 100.0)))

    # Tests for str5_hsv (add two)
    introcs.assert_equals('(0.000, 1.000, 0.500)',
                          a2.str5_hsv(introcs.HSV(0.0, 1.0, 0.5)))
    introcs.assert_equals('(0.000, 0.314, 1.000)',
                          a2.str5_hsv(introcs.HSV(0.0, 0.313725490196, 1.0)))
    introcs.assert_equals('(12.00, 0.468, 0.325)',
                          a2.str5_hsv(introcs.HSV(12, 0.46792, 0.32456)))
    introcs.assert_equals(
        '(225.3, 0.151, 0.786)',
        a2.str5_hsv(introcs.HSV(225.298, 0.1512256, 0.78610)))
Esempio n. 5
0
def test_cmyk_to_rgb():
    """
    Test translation function cmyk_to_rgb
    """
    cmyk = introcs.CMYK(0.000, 100.0, 100.0, 0.000)
    rgb = a2.cmyk_to_rgb(cmyk)
    introcs.assert_equals('0.000', a2.str5(rgb.blue))
    introcs.assert_equals('255.0', a2.str5(rgb.red))
    introcs.assert_equals('0.000', a2.str5(rgb.green))

    cmyk = introcs.CMYK(88.44, 76.99, 5.528, 21.96)
    rgb = a2.cmyk_to_rgb(cmyk)
    introcs.assert_equals('188.0', a2.str5(rgb.blue))
    introcs.assert_equals('46.00', a2.str5(rgb.green))
    introcs.assert_equals('23.00', a2.str5(rgb.red))

    cmyk = introcs.CMYK(46.98, 55.10, 100.0, 2.122)
    rgb = a2.cmyk_to_rgb(cmyk)
    introcs.assert_equals('0.000', a2.str5(rgb.blue))
    introcs.assert_equals('112.0', a2.str5(rgb.green))
    introcs.assert_equals('132.0', a2.str5(rgb.red))
Esempio n. 6
0
    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()
Esempio n. 7
0
def test_cmyk_to_rgb():
    """
    Test translation function cmyk_to_rgb
    """
    print('Testing cmyk_to_rgb')
    # ADD TESTS TO ME
    #all zero
    cmyk = introcs.CMYK(0.0, 0.0, 0.0, 0.0)
    rgb = a3.cmyk_to_rgb(cmyk)
    introcs.assert_equals(255, round(rgb.red,3))
    introcs.assert_equals(255,round(rgb.green,3))
    introcs.assert_equals(255,round(rgb.blue,3))
    # only black is max
    cmyk = introcs.CMYK(0.0, 0.0, 0.0, 100.0)
    rgb = a3.cmyk_to_rgb(cmyk)
    introcs.assert_equals(0, round(rgb.red,3))
    introcs.assert_equals(0,round(rgb.green,3))
    introcs.assert_equals(0,round(rgb.blue,3))
    # when all values are max
    cmyk = introcs.CMYK(100.0, 100.0, 100.0, 100.0)
    rgb = a3.cmyk_to_rgb(cmyk)
    introcs.assert_equals(0, round(rgb.red,3))
    introcs.assert_equals(0,round(rgb.green,3))
    introcs.assert_equals(0,round(rgb.blue,3))
    # test M is greatest
    cmyk = introcs.CMYK(0.0, 80.184, 24.424, 14.902)
    rgb = a3.cmyk_to_rgb(cmyk)
    introcs.assert_equals(217, round(rgb.red,3))
    introcs.assert_equals(43,round(rgb.green,3))
    introcs.assert_equals(164,round(rgb.blue,3))
    #test C is greatest
    cmyk = introcs.CMYK(89.0, 11.0, 23.0, 50.0)
    rgb = a3.cmyk_to_rgb(cmyk)
    introcs.assert_equals(14, round(rgb.red,3))
    introcs.assert_equals(113,round(rgb.green,3))
    introcs.assert_equals(98,round(rgb.blue,3))
    print('Tests for cmyk_to_rgb passed')
Esempio n. 8
0
    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()
Esempio n. 9
0
def test_str5_color():
    """
    Test the str5 functions for cmyk and hsv.
    """
    introcs.assert_equals('(98.45, 25.36, 72.80, 1.000)',
                              a2.str5_cmyk(introcs.CMYK(98.448, 25.362, 72.8, 1.0)))