コード例 #1
0
def test_rgb_to_cmyk():
    """Test rgb_to_cmyk"""
    rgb = colormodel.RGB(255, 255, 255)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.magenta))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.yellow))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.black))

    rgb = colormodel.RGB(0, 0, 0)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.magenta))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.yellow))
    cornelltest.assert_equals('100.0', a3.str5(cmyk.black))

    rgb = colormodel.RGB(217, 43, 164)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornelltest.assert_equals('80.18', a3.str5(cmyk.magenta))
    cornelltest.assert_equals('24.42', a3.str5(cmyk.yellow))
    cornelltest.assert_equals('14.90', a3.str5(cmyk.black))

    rgb = colormodel.RGB(50, 123, 43)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals('59.35', a3.str5(cmyk.cyan))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.magenta))
    cornelltest.assert_equals('65.04', a3.str5(cmyk.yellow))
    cornelltest.assert_equals('51.76', a3.str5(cmyk.black))
コード例 #2
0
def test_rgb_to_hsv():
    """Test translation function rgb_to_hsv"""
    #MAX = MIN
    rgb = colormodel.RGB(255, 255, 255)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('0.000', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.000', a3.str5(hsv.saturation))
    cornelltest.assert_equals('1.000', a3.str5(hsv.value))

    rgb = colormodel.RGB(0, 0, 0)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('0.000', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.000', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.000', a3.str5(hsv.value))

    rgb = colormodel.RGB(100, 100, 100)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('0.000', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.000', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.392', a3.str5(hsv.value))

    #MAX = R and G >= B
    rgb = colormodel.RGB(200, 150, 100)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('30.00', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.500', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.784', a3.str5(hsv.value))

    rgb = colormodel.RGB(242, 67, 67)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('0.000', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.723', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.949', a3.str5(hsv.value))

    rgb = colormodel.RGB(255, 0, 0)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('0.000', a3.str5(hsv.hue))
    cornelltest.assert_equals('1.000', a3.str5(hsv.saturation))
    cornelltest.assert_equals('1.000', a3.str5(hsv.value))

    #MAX = R and G < B
    rgb = colormodel.RGB(123, 87, 99)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('340.0', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.293', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.482', a3.str5(hsv.value))

    #MAX = G
    rgb = colormodel.RGB(1, 6, 2)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('132.0', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.833', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.024', a3.str5(hsv.value))

    #MAX = B
    rgb = colormodel.RGB(12, 64, 242)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('226.4', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.950', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.949', a3.str5(hsv.value))
コード例 #3
0
def complement_rgb(rgb):
    """Returns: the complement of color rgb.
    
    Precondition: rgb is an RGB object"""
    assert (type(rgb) == colormodel.RGB
            ), 'Value ' + ` rgb ` + ' is not a RGB object'
    return colormodel.RGB(255 - rgb.red, 255 - rgb.green, 255 - rgb.blue)
コード例 #4
0
def cmyk_to_rgb(cmyk):
    """Returns : color CMYK in space RGB.

    Formulae from en.wikipedia.org/wiki/CMYK_color_model.
   
    Parameter cmyk: the color to convert to a RGB object
    Precondition: cmyk is an CMYK object."""

    # Divide all rgb values by 100 to change range from 0..100.0 to 0..1.0
    c = cmyk.cyan / 100
    m = cmyk.magenta / 100
    y = cmyk.yellow / 100
    k = cmyk.black / 100

    #Cmyk to rgb conversion formulas
    r = ((1 - c) * (1 - k)) * 255
    g = ((1 - m) * (1 - k)) * 255
    b = ((1 - y) * (1 - k)) * 255

    #Round numbers to convert values to int
    r1 = int(round(r, 0))
    g1 = int(round(g, 0))
    b1 = int(round(b, 0))

    #Return rgb values
    return colormodel.RGB(r1, g1, b1)
コード例 #5
0
def complement_rgb(rgb):
    """Returns: the complement of color rgb.
    
    Parameter rgb: the color to complement
    Precondition: rgb is an RGB object"""

    return colormodel.RGB((255 - rgb.red), (255 - rgb.green), (255 - rgb.blue))
コード例 #6
0
def complement_rgb(rgb):
    """Returns: the complement of color rgb.
    
    Parameter rgb: the color to complement
    Precondition: rgb is an RGB object"""
    # THIS IS WRONG.  FIX IT
    return colormodel.RGB(255 - rgb.red, 255 - rgb.green, 255 - rgb.blue)
コード例 #7
0
 def on_rgb_press(self,r,g,b):
     """Call back to rgb button"""
     self.rgb = colormodel.RGB(r, g, b)
     self.hsv = a3.rgb_to_hsv(self.rgb)
     assert (self.hsv == None or type(self.hsv) == colormodel.HSV), 'rgb_to_hsv does not return a HSV object'
     self.cmyk = a3.rgb_to_cmyk(self.rgb)
     assert (self.cmyk == None or type(self.cmyk) == colormodel.CMYK), 'rgb_to_cmyk does not return a CMYK object'
     self.update()
コード例 #8
0
def hsv_to_rgb(hsv):
    #add 6 cases
    """Returns: color in RGB color space.
    
    Formulae from http://en.wikipedia.org/wiki/HSV_color_space.
    
    Parameter hsv: the color to convert to a RGB object
    Precondition: hsv is an HSV object."""
    rgb = colormodel.RGB(0, 0, 0)
    h = hsv.hue
    s = hsv.saturation
    v = hsv.value

    # r=rgb.red
    #g=rgb.green
    #b=rgb.blue

    hi = math.floor(h / 60)
    f = (h / 60) - hi
    p = v * (1 - s)
    q = v * (1 - (f * s))
    t = v * (1 - (1 - f) * s)

    if hi == 0:
        r = v
        g = t
        b = p

    elif hi == 1:
        r = q
        g = v
        b = p

    elif hi == 2:
        r = p
        g = v
        b = t

    elif hi == 3:
        r = p
        g = q
        b = v

    elif hi == 4:
        r = t
        g = p
        b = v

    elif hi == 5:
        r = v
        g = p
        b = q

    rgb.red = int(round(r * 255, 0))
    rgb.green = int(round(g * 255, 0))
    rgb.blue = int(round(b * 255, 0))

    return rgb  # Stub
コード例 #9
0
 def register(self):
     """Initialize color values and force refresh"""
     active = True
     self.rgb = colormodel.RGB(0, 255, 0)
     self.cmyk = a3.rgb_to_cmyk(self.rgb)
     assert (self.cmyk == None or type(self.cmyk) == colormodel.CMYK), 'rgb_to_cmyk does not return a CMYK object'
     self.hsv = a3.rgb_to_hsv(self.rgb)
     assert (self.hsv == None or type(self.hsv) == colormodel.HSV), 'rgb_to_hsv does not return a HSV object'
     self.update()
コード例 #10
0
ファイル: a3.py プロジェクト: mfx22/CS1110
def complement_rgb(rgb):
    """Returns: the complement of color rgb.
    
    Parameter rgb: the color to complement
    Precondition: rgb is an RGB object"""
    color = colormodel.RGB(0, 0, 0)
    color.red = 255 - rgb.red
    color.green = 255 - rgb.green
    color.blue = 255 - rgb.blue
    return color
コード例 #11
0
ファイル: a3test.py プロジェクト: qbeightol/cs1110
def test_rgb_to_cmyk():
    """Test rgb_to_cmyk"""
    #Test rgb_to_cmyk() when C = M = Y = 0:
    rgb = colormodel.RGB(255, 255, 255)
    cmyk = a3.rgb_to_cmyk(rgb)
    cunittest.assert_equals("0.000", a3.round5(cmyk.cyan))
    cunittest.assert_equals("0.000", a3.round5(cmyk.magenta))
    cunittest.assert_equals("0.000", a3.round5(cmyk.yellow))
    cunittest.assert_equals("0.000", a3.round5(cmyk.black))

    #Test rgb_to_cmyk() when C' = M' = Y' = 1:
    rgb = colormodel.RGB(0, 0, 0)
    cmyk = a3.rgb_to_cmyk(rgb)
    cunittest.assert_equals("0.000", a3.round5(cmyk.cyan))
    cunittest.assert_equals("0.000", a3.round5(cmyk.magenta))
    cunittest.assert_equals("0.000", a3.round5(cmyk.yellow))
    cunittest.assert_equals("100.0", a3.round5(cmyk.black))

    #Test rgb_to_cmyk() when C' = K (=> C = 0):
    rgb = colormodel.RGB(217, 43, 164)
    cmyk = a3.rgb_to_cmyk(rgb)
    cunittest.assert_equals("0.000", a3.round5(cmyk.cyan))
    cunittest.assert_equals("80.18", a3.round5(cmyk.magenta))
    cunittest.assert_equals("24.42", a3.round5(cmyk.yellow))
    cunittest.assert_equals("14.90", a3.round5(cmyk.black))

    #Test rgb_to_cmyk() when M' = K (=> M = 0):
    rgb = colormodel.RGB(1, 100, 1)
    cmyk = a3.rgb_to_cmyk(rgb)
    cunittest.assert_equals("99.00", a3.round5(cmyk.cyan))
    cunittest.assert_equals("0.000", a3.round5(cmyk.magenta))
    cunittest.assert_equals("99.00", a3.round5(cmyk.yellow))
    cunittest.assert_equals("60.78", a3.round5(cmyk.black))

    #Test rgb_to_cmyk() when Y' = K (=> Y = 0):
    rgb = colormodel.RGB(101, 40, 141)
    cmyk = a3.rgb_to_cmyk(rgb)
    cunittest.assert_equals("28.37", a3.round5(cmyk.cyan))
    cunittest.assert_equals("71.63", a3.round5(cmyk.magenta))
    cunittest.assert_equals("0.000", a3.round5(cmyk.yellow))
    cunittest.assert_equals("44.71", a3.round5(cmyk.black))
コード例 #12
0
 def on_rgb_slide(self,r,g,b):
     """Call back 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 = colormodel.RGB(red, green, blue)
     self.hsv = a3.rgb_to_hsv(self.rgb)
     assert (self.hsv == None or type(self.hsv) == colormodel.HSV), 'rgb_to_hsv does not return a HSV object'
     self.cmyk = a3.rgb_to_cmyk(self.rgb)
     assert (self.cmyk == None or type(self.cmyk) == colormodel.CMYK), 'rgb_to_cmyk does not return a CMYK object'
     self.update()
コード例 #13
0
def test_complement():
    """Test function complement"""
    cornelltest.assert_equals(colormodel.RGB(255 - 250, 255 - 0, 255 - 71),
                              a3.complement_rgb(colormodel.RGB(250, 0, 71)))
    cornelltest.assert_equals(colormodel.RGB(255 - 255, 255 - 255, 255 - 255),
                              a3.complement_rgb(colormodel.RGB(255, 255, 255)))
    cornelltest.assert_equals(colormodel.RGB(255 - 0, 255 - 0, 255 - 0),
                              a3.complement_rgb(colormodel.RGB(0, 0, 0)))
コード例 #14
0
def hsv_to_rgb(hsv):
    """Returns: color in RGB color space.
    
    Formulae from http://en.wikipedia.org/wiki/HSV_color_space.
    
    Parameter hsv: the color to convert to a RGB object
    Precondition: hsv is an HSV object."""

    #Attributes of hsv
    h = hsv.hue
    s = hsv.saturation
    v = hsv.value

    #Computed values for conversion
    hi = math.floor(h / 60)
    f = h / 60 - hi
    p = v * (1 - s)
    q = v * (1 - f * s)
    t = v * (1 - (1 - f) * s)

    if hi == 0:
        r = v
        g = t
        b = p
    elif hi == 1:
        r = q
        g = v
        b = p
    elif hi == 2:
        r = p
        g = v
        b = t
    elif hi == 3:
        r = p
        g = q
        b = v
    elif hi == 4:
        r = t
        g = p
        b = v
    elif hi == 5:
        r = v
        g = p
        b = q

    #Round numbers to convert values to int
    r1 = int(round(r * 255.0, 0))
    g1 = int(round(g * 255.0, 0))
    b1 = int(round(b * 255.0, 0))

    return colormodel.RGB(r1, g1, b1)
コード例 #15
0
def test_rgb_to_cmyk():
    """Test rgb_to_cmyk"""
    rgb = colormodel.RGB(255, 255, 255)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.magenta))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.yellow))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.black))

    rgb = colormodel.RGB(0, 0, 0)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.magenta))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.yellow))
    cornelltest.assert_equals('100.0', a3.str5(cmyk.black))

    rgb = colormodel.RGB(217, 43, 164)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornelltest.assert_equals('80.18', a3.str5(cmyk.magenta))
    cornelltest.assert_equals('24.42', a3.str5(cmyk.yellow))
    cornelltest.assert_equals('14.90', a3.str5(cmyk.black))

    rgb = colormodel.RGB(1, 3, 9)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals('88.89', a3.str5(cmyk.cyan))
    cornelltest.assert_equals('66.67', a3.str5(cmyk.magenta))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.yellow))
    cornelltest.assert_equals('96.47', a3.str5(cmyk.black))

    rgb = colormodel.RGB(50, 80, 45)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornelltest.assert_equals('37.50', a3.str5(cmyk.cyan))
    cornelltest.assert_equals('0.000', a3.str5(cmyk.magenta))
    cornelltest.assert_equals('43.75', a3.str5(cmyk.yellow))
    cornelltest.assert_equals('68.63', a3.str5(cmyk.black))
コード例 #16
0
def hsv_to_rgb(hsv):
    """Returns: color in RGB color space.
    
    Formulae from http://en.wikipedia.org/wiki/HSV_color_space.
    
    Precondition: hsv is an HSV object."""

    assert (type(hsv) == colormodel.HSV), ('Value ' + ` hsv ` +
                                           ' is not an RGB object')

    H = hsv.hue
    S = hsv.saturation
    V = hsv.value

    H_sub_i = math.floor(H / 60)
    f = H / 60 - H_sub_i
    p = V * (1 - S)
    q = V * (1 - f * S)
    t = V * (1 - (1 - f) * S)

    if H_sub_i == 0:
        R = V
        G = t
        B = p
    elif H_sub_i == 1:
        R = q
        G = V
        B = p
    elif H_sub_i == 2:
        R = p
        G = V
        B = t
    elif H_sub_i == 3:
        R = p
        G = q
        B = V
    elif H_sub_i == 4:
        R = t
        G = p
        B = V
    else:
        R = V
        G = p
        B = q

    return colormodel.RGB(int(round(255 * R)), int(round(255 * G)),
                          int(round(255 * B)))
コード例 #17
0
ファイル: a3test.py プロジェクト: qbeightol/cs1110
def test_rgb_to_hsv():
    """Test translation function rgb_to_hsv"""
    #Test rgb_to_hsv when MAX = MIN and MAX = 0:
    rgb = colormodel.RGB(0, 0, 0)
    hsv = a3.rgb_to_hsv(rgb)
    cunittest.assert_equals("0.000", a3.round5(hsv.hue))
    cunittest.assert_equals("0.000", a3.round5(hsv.saturation))
    cunittest.assert_equals("0.000", a3.round5(hsv.value))

    #Test rgb_to_hsv when MAX = MIN and MAX <> 0:
    rgb = colormodel.RGB(100, 100, 100)
    hsv = a3.rgb_to_hsv(rgb)
    cunittest.assert_equals("0.000", a3.round5(hsv.hue))
    cunittest.assert_equals("0.000", a3.round5(hsv.saturation))
    cunittest.assert_equals("0.392", a3.round5(hsv.value))

    #Test rgb_to_hsv when MAX = R and G >=B:
    rgb = colormodel.RGB(161, 42, 42)
    hsv = a3.rgb_to_hsv(rgb)
    cunittest.assert_equals("0.000", a3.round5(hsv.hue))
    cunittest.assert_equals("0.739", a3.round5(hsv.saturation))
    cunittest.assert_equals("0.631", a3.round5(hsv.value))

    rgb = colormodel.RGB(161, 72, 42)
    hsv = a3.rgb_to_hsv(rgb)
    cunittest.assert_equals("15.13", a3.round5(hsv.hue))
    cunittest.assert_equals("0.739", a3.round5(hsv.saturation))
    cunittest.assert_equals("0.631", a3.round5(hsv.value))

    #Test rgb_to_hsv when MAX = R and G < B:
    rgb = colormodel.RGB(161, 42, 72)
    hsv = a3.rgb_to_hsv(rgb)
    cunittest.assert_equals("344.9", a3.round5(hsv.hue))
    cunittest.assert_equals("0.739", a3.round5(hsv.saturation))
    cunittest.assert_equals("0.631", a3.round5(hsv.value))

    #Test rgb_to_hsv when MAX = G:
    rgb = colormodel.RGB(17, 101, 19)
    hsv = a3.rgb_to_hsv(rgb)
    cunittest.assert_equals("121.4", a3.round5(hsv.hue))
    cunittest.assert_equals("0.832", a3.round5(hsv.saturation))
    cunittest.assert_equals("0.396", a3.round5(hsv.value))

    #Test rgb_to_hsv when MAX = B:
    rgb = colormodel.RGB(21, 100, 255)
    hsv = a3.rgb_to_hsv(rgb)
    cunittest.assert_equals("219.7", a3.round5(hsv.hue))
    cunittest.assert_equals("0.918", a3.round5(hsv.saturation))
    cunittest.assert_equals("1.000", a3.round5(hsv.value))
コード例 #18
0
ファイル: a3.py プロジェクト: mfx22/CS1110
def cmyk_to_rgb(cmyk):
    """Returns : color CMYK in space RGB.

    Formulae from en.wikipedia.org/wiki/CMYK_color_model.
   
    Parameter cmyk: the color to convert to a RGB object
    Precondition: cmyk is an CMYK object."""
    rgb = colormodel.RGB(0, 0, 0)

    c = cmyk.cyan / 100.0
    m = cmyk.magenta / 100.0
    y = cmyk.yellow / 100.0
    k = cmyk.black / 100.0

    rgb.red = int(round((1 - c) * (1 - k) * 255, 0))
    rgb.green = int(round((1 - m) * (1 - k) * 255, 0))
    rgb.blue = int(round((1 - y) * (1 - k) * 255, 0))

    return rgb
コード例 #19
0
def test_rgb_to_hsv():
    """Test translation function rgb_to_hsv"""
    rgb= colormodel.RGB(20,20,20)
    hsv= a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals( '0.000', a3.str5(hsv.hue))
    
    
    rgb= colormodel.RGB(100,25,20)
    hsv= a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals( '3.750', a3.str5(hsv.hue))
    cornelltest.assert_equals( '0.800', a3.str5(hsv.saturation))
    cornelltest.assert_equals( '0.392', a3.str5(hsv.value))
    
    rgb= colormodel.RGB(200,50,100)
    hsv= a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals( '340.0', a3.str5(hsv.hue))
    cornelltest.assert_equals( '0.750', a3.str5(hsv.saturation))
    cornelltest.assert_equals( '0.784', a3.str5(hsv.value))
    
   
    
    rgb= colormodel.RGB(20,100,20)
    hsv= a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals( '120.0', a3.str5(hsv.hue))
    cornelltest.assert_equals( '0.800', a3.str5(hsv.saturation))
    cornelltest.assert_equals( '0.392', a3.str5(hsv.value))
    
    rgb= colormodel.RGB(20,20,100)
    hsv= a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals( '240.0', a3.str5(hsv.hue))
    cornelltest.assert_equals( '0.800', a3.str5(hsv.saturation))
    cornelltest.assert_equals( '0.392', a3.str5(hsv.value))
    
    rgb= colormodel.RGB(0,0,0)
    hsv= a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals( '0.000', a3.str5(hsv.hue))
    cornelltest.assert_equals( '0.000', a3.str5(hsv.saturation))
    cornelltest.assert_equals( '0.000', a3.str5(hsv.value))
    
    rgb= colormodel.RGB(20,25,25)
    hsv= a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals( '180.0', a3.str5(hsv.hue))
    cornelltest.assert_equals( '0.200', a3.str5(hsv.saturation))
    cornelltest.assert_equals( '0.098', a3.str5(hsv.value))
コード例 #20
0
ファイル: a3test.py プロジェクト: qbeightol/cs1110
def test_to_strings():
    """Test toString methods"""
    #Test rgb_to_string:()
    cunittest.assert_equals("(30, 240, 230)",
                            a3.rgb_to_string(colormodel.RGB(30, 240, 230)))

    #Test cmyk_to_string()
    cunittest.assert_equals("(10.00, 11.00, 20.00, 12.00)",
                            a3.cmyk_to_string(colormodel.CMYK(10, 11, 20, 12)))
    #Test that cmyk_to_string() uses round5(), not `truncate5()`:
    cunittest.assert_equals(
        "(10.01, 11.00, 20.01, 12.00)",
        a3.cmyk_to_string(colormodel.CMYK(10.005, 11.0045, 20.009, 12.001)))

    #Test hsv_to_string()
    cunittest.assert_equals("(100.0, 1.000, 1.000)",
                            a3.hsv_to_string(colormodel.HSV(100, 1, 1)))
    #Test that hsv_to_string uses round5(), not `truncate5()`:
    cunittest.assert_equals(
        "(100.0, 0.500, 0.499)",
        a3.hsv_to_string(colormodel.HSV(99.999, .4996, .4994)))
コード例 #21
0
def cmyk_to_rgb(cmyk):
    """Returns : color CMYK in space RGB.

    Formulae from en.wikipedia.org/wiki/CMYK_color_model.
    
    Precondition: cmyk is an CMYK object."""

    assert (type(cmyk) == colormodel.CMYK), ('Value ' + ` cmyk ` +
                                             ' is not an RGB object')

    C = cmyk.cyan / 100.0
    M = cmyk.magenta / 100.0
    Y = cmyk.yellow / 100.0
    K = cmyk.black / 100.0

    R = (1 - C) * (1 - K)
    G = (1 - M) * (1 - K)
    B = (1 - Y) * (1 - K)

    return colormodel.RGB(int(round(255 * R)), int(round(255 * G)),
                          int(round(255 * B)))
コード例 #22
0
def cmyk_to_rgb(cmyk):
    """Returns : color CMYK in space RGB.

    Formulae from en.wikipedia.org/wiki/CMYK_color_model.
   
    Parameter cmyk: the color to convert to a RGB object
    Precondition: cmyk is an CMYK object."""
    # The CMYK numbers are in the range 0.0..100.0.  Deal with them in the
    # same way as the RGB numbers in rgb_to_cmyk()
    rgb = colormodel.RGB(0, 0, 0)

    c = cmyk.cyan / 100.0
    m = cmyk.magenta / 100.0
    y = cmyk.yellow / 100.0
    k = cmyk.black / 100.0

    rgb.red = int(round((1 - c) * (1 - k) * 255, 0))
    rgb.green = int(round((1 - m) * (1 - k) * 255, 0))
    rgb.blue = int(round((1 - m) * (1 - k) * 255, 0))

    return rgb  # Stub
コード例 #23
0
def test_rgb_to_hsv():
    """Test translation function rgb_to_hsv"""
    rgb = colormodel.RGB(255, 255, 255)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('0.000', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.000', a3.str5(hsv.saturation))
    cornelltest.assert_equals('1.000', a3.str5(hsv.value))

    rgb = colormodel.RGB(245, 87, 7)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('20.17', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.971', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.961', a3.str5(hsv.value))

    rgb = colormodel.RGB(99, 33, 33)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('0.000', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.667', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.388', a3.str5(hsv.value))

    rgb = colormodel.RGB(54, 2, 3)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('358.8', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.963', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.212', a3.str5(hsv.value))

    rgb = colormodel.RGB(99, 100, 98)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('90.00', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.020', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.392', a3.str5(hsv.value))

    rgb = colormodel.RGB(100, 1, 150)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('279.9', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.993', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.588', a3.str5(hsv.value))

    rgb = colormodel.RGB(0, 0, 0)
    hsv = a3.rgb_to_hsv(rgb)
    cornelltest.assert_equals('0.000', a3.str5(hsv.hue))
    cornelltest.assert_equals('0.000', a3.str5(hsv.saturation))
    cornelltest.assert_equals('0.000', a3.str5(hsv.value))
コード例 #24
0
ファイル: a3test.py プロジェクト: qbeightol/cs1110
def test_complement():
    """Test function complement"""
    cunittest.assert_equals(colormodel.RGB(255 - 250, 255 - 0, 255 - 71),
                            a3.complement_rgb(colormodel.RGB(250, 0, 71)))
コード例 #25
0
ファイル: a3app.py プロジェクト: qbeightol/cs1110
class ColorWidget(BoxLayout):
    """Instances represent the top level widget"""
    active = True
    rgb = colormodel.RGB(0, 255, 0)
    cmyk = None
    hsv = None

    top = ObjectProperty(None)
    bot = ObjectProperty(None)

    def register(self):
        """Initialize color values and force refresh"""
        self.rgb = colormodel.RGB(0, 255, 0)
        self.cmyk = a3.rgb_to_cmyk(self.rgb)
        assert (self.cmyk == None or type(self.cmyk) == colormodel.CMYK
                ), 'rgb_to_cmyk does not return a CMYK object'
        self.hsv = a3.rgb_to_hsv(self.rgb)
        assert (self.hsv == None or type(self.hsv)
                == colormodel.HSV), 'rgb_to_hsv does not return a HSV object'
        self.update()

    def update(self):
        """Force refresh of top panel on update"""
        self.active = False
        self.top.update(self.rgb, self.cmyk, self.hsv)
        self.active = True

    def on_rgb_press(self, r, g, b):
        """Call back to rgb button"""
        self.bot.clear()
        self.rgb = colormodel.RGB(r, g, b)
        self.hsv = a3.rgb_to_hsv(self.rgb)
        assert (self.hsv == None or type(self.hsv)
                == colormodel.HSV), 'rgb_to_hsv does not return a HSV object'
        self.cmyk = a3.rgb_to_cmyk(self.rgb)
        assert (self.cmyk == None or type(self.cmyk) == colormodel.CMYK
                ), 'rgb_to_cmyk does not return a CMYK object'
        self.update()

    def on_rgb_slide(self, r, g, b):
        """Call back 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 = colormodel.RGB(red, green, blue)
        self.hsv = a3.rgb_to_hsv(self.rgb)
        assert (self.hsv == None or type(self.hsv)
                == colormodel.HSV), 'rgb_to_hsv does not return a HSV object'
        self.cmyk = a3.rgb_to_cmyk(self.rgb)
        assert (self.cmyk == None or type(self.cmyk) == colormodel.CMYK
                ), 'rgb_to_cmyk does not return a CMYK object'
        self.update()

    def on_cmyk_press(self, c, m, y, k):
        """Call back to cmyk button"""
        self.bot.clear()
        self.cmyk = colormodel.CMYK(c, m, y, k)
        temp = a3.cmyk_to_rgb(self.cmyk)
        assert (temp == None or type(temp)
                == colormodel.RGB), 'cmyk_to_rgb does not return a RGB object'
        self.rgb = self.rgb if temp is None else temp
        self.hsv = a3.rgb_to_hsv(self.rgb)
        assert (self.hsv == None or type(self.hsv)
                == colormodel.HSV), 'rgb_to_hsv does not return a HSV object'
        self.update()

    def on_cmyk_slide(self, c, m, y, k):
        """Call back 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 = colormodel.CMYK(cyan, magenta, yellow, black)
        temp = a3.cmyk_to_rgb(self.cmyk)
        assert (temp == None or type(temp)
                == colormodel.RGB), 'cmyk_to_rgb does not return a RGB object'
        self.rgb = self.rgb if temp is None else temp
        self.hsv = a3.rgb_to_hsv(self.rgb)
        assert (self.hsv == None or type(self.hsv)
                == colormodel.HSV), 'rgb_to_hsv does not return a HSV object'
        self.update()

    def on_hsv_press(self, h, s, v):
        """Call back to hsv button"""
        self.bot.clear()
        self.hsv = colormodel.HSV(h, s, v)
        temp = a3.hsv_to_rgb(self.hsv)
        assert (temp == None or type(temp)
                == colormodel.RGB), 'hsv_to_rgb does not return a RGB object'
        self.rgb = self.rgb if temp is None else temp
        self.cmyk = a3.rgb_to_cmyk(self.rgb)
        assert (self.cmyk == None or type(self.cmyk) == colormodel.CMYK
                ), 'rgb_to_cmyk does not return a CMYK object'
        self.update()

    def on_hsv_slide(self, h, s, v):
        """Call back to hsv sliders"""
        if not self.active:
            return
        hue = h / 100.0
        sat = s / 100.0
        val = v / 100.0
        self.hsv = colormodel.HSV(hue, sat, val)
        temp = a3.hsv_to_rgb(self.hsv)
        assert (temp == None or type(temp)
                == colormodel.RGB), 'hsv_to_rgb does not return a RGB object'
        self.rgb = self.rgb if temp is None else temp
        self.cmyk = a3.rgb_to_cmyk(self.rgb)
        assert (self.cmyk == None or type(self.cmyk) == colormodel.CMYK
                ), 'rgb_to_cmyk does not return a CMYK object'
        self.update()
コード例 #26
0
ファイル: graphics.py プロジェクト: qbeightol/cs1110
class GLabel(GObject):
    """Instance represents an (uneditable) text label in `GameView`
    
    The attribute `text` defines the content of this image.  Uses of
    the escape character '\\n' will result in a label that spans multiple
    lines.  The label includes both the text, and a rectangular backdrop
    with bottom left corner at `pos` and width and height `size`.  The
    background color of this rectangle is `fillcolor`, while `linecolor`
    is the color of the text.
    
    The text itself is aligned within this rectangle according to the
    attributes `halign` and `valign`.  See the documentation of these
    attributes for how alignment works.  There are also attributes
    to change the point size, font style, and font name of the text.
    The `size` attribute of this label will grow to ensure that the
    text will fit in the rectangle, no matter the font or point size.
    
    To change the font, you need a .ttf (TrueType Font) file in the
    Fonts folder; refer to the font by filename, including the .ttf.
    If you give no name, it will use the default Kivy font.  The
    `bold` attribute only works for the default Kivy font; for other
    fonts you will need the .ttf file for the bold version of that
    font.  See `ComicSans.ttf` and `ComicSansBold.ttf` for an example."""
    # Fields.  See the associated property.
    _valign = 'bottom'
    _halign = 'left'
    
    # Override the fill color
    _fillcolor = colormodel.RGB(0,0,0,0)  # fill color field
    _kivy_fill_color = ListProperty([0,0,0,0]) # Kivy representation of fill color

    # Interior Kivy label.  Hidden field with no property.
    _label = None

    @property
    def font_size(self):
        """Size of the text font in points.
        
        **Invariant**: A positive number (int or float)"""
        return self._label.font_size

    @font_size.setter
    def font_size(self,value):
        assert type(value) in (int,float), `value`+' is not a number'
        self._label.font_size = value
        self._label.texture_update()

    @property
    def font_name(self):
        """File name for the .ttf file to use as a font
        
        **Invariant**: string referring to a .ttf file in folder Fonts"""
        return self._label.font_name

    @font_name.setter
    def font_name(self,value):
        assert type(value) == str, `value`+' is not a string'
        self._label.font_name = value
        self._label.texture_update()

    @property
    def bold(self):
        """Boolean indicating whether or not the text should be bold.
        
        Only works on the default Kivy font.  Does not work on custom
        .ttf files.  In that case, you need the bold version of the
        .ttf file.  See `ComicSans.ttf` and `ComicSansBold.ttf` for
        an example.
        
        **Invariant**: boolean"""
        return self._label.bold

    @bold.setter
    def bold(self,value):
        assert type(value) == bool, `value`+' is not a bool'
        self._label.bold = value
        self._label.texture_update()

    @property
    def text(self):
        """Text for this label.
        
        The text in the label is displayed as a single line, or broken
        up into multiple lines in the presence of the escape character
        '\\n'. The `size` attribute of this label grows to make sure
        that the entire text fits inside of the rectangle.
        
        **Invariant**: string"""
        return self._label.text
    
    @text.setter
    def text(self,value):
        assert type(value) == str, `value`+' is not a string'
        self._label.text = value
        self._label.texture_update()

    @property
    def halign(self):
        """Horizontal alignment for this label.
        
        The text is anchored inside of the label rectangle on either the
        left, the right or the center.  This means that as the size of
        the label increases, the text will still stay rooted at that
        anchor.
        
        **Invariant**: one of 'left', 'right', or 'center'"""
        return self._halign
    
    @halign.setter
    def halign(self,value):
        assert value in ('left','right','center'), `value`+' is not a valid horizontal alignment'
        self._halign = value
        if not self._label is None:
            self._label.halign = value

    @property
    def valign(self):
        """Vertical alignment for this label.
        
        The text is anchored inside of the label rectangle at either the
        top, the bottom or the middle.  This means that as the size of
        the label increases, the text will still stay rooted at that
        anchor.
        
        **Invariant**: one of 'top', 'bottom', or 'middle'"""
        return self._valign
    
    @valign.setter
    def valign(self,value):
        assert value in ('top','middle','bottom'), `value`+' is not a valid vertical alignment'
        self._valign = value
        if not self._label is None:
            self._label.valign = value

    @property
    def linecolor(self):
        """The text color for this label.
        
        Overrides `linecolor` property in `GObject`
            
        **Invariant**: Must be a RGB or HSV object from module `colormodel`."""
        return self._linecolor
    
    @linecolor.setter
    def linecolor(self,value):
        assert type(value) in (colormodel.RGB, colormodel.HSV), `value`+' is not a valid color'
        self._linecolor = value
        if not self._label is None:
            self._label.color = value.glColor()
    
    def __init__(self,**keywords):
        """**Constructor**: creates a new text label.
        
            :param keywords: dictionary of keyword arguments 
            **Precondition**: See below.
        
        To use the constructor for this class, you should provide
        it with a list of keyword arguments that initialize various
        attributes. For example, to create a label containing the
        word 'Hello', use the constructor call
        
            GLabel(text='Hello')
        
        This class supports the same keywords as `GObject`, as well
        as additional attributes for the text properties (e.g. font
        size and name)."""
        self._label = Label(**keywords)
        self._label.size_hint = (None,None)
        super(GLabel,self).__init__(**keywords)
        self.add_widget(self._label)
        self.size = self._label.size

        if 'halign' in keywords:
            self.halign = keywords['halign']

        if 'valign' in keywords:
            self.valign = keywords['valign']

        if not 'linecolor' in keywords:
            self.linecolor = colormodel.BLACK
            
        self._label.bind(texture_size=self._resize)
        self.bind(pos=self._resize)
        self._resize()
    
    # Compute the size and pos from the text texture.
    # Also align the internal label in the rectangle.
    def _resize(self,instance=None,value=None):
        self._label.size = self._label.texture_size
        
        # Resize the outside if necessary
        width = max(self.width,self._label.width)
        height = max(self.height,self._label.height)
        
        # Reset to horizontal anchor position.
        if self._halign == 'left':
            self.width = width
        elif self._halign == 'center':
            cx = self.center_x
            self.width = width
            self.center_x = cx
        else:
            right = self.right
            self.width = width
            self.right = right            

        # Reset to vertical anchor position.
        if self._valign == 'top':
            top = self.top
            self.height = height
            self.top = top
        elif self._valign == 'middle':
            cy = self.center_y
            self.height = height
            self.center_y = cy
        else:
            self.height = height
        
        # Internal Horizontal placement
        if self._halign == 'left':
            self._label.x = self.x
        elif self._halign == 'center':
            self._label.center_x = self.center_x
        else: # 'ightr'
            self._label.right = self.right
        
        # Internal Vertical placement
        if self._valign == 'top':
            self._label.top = self.top
        elif self._valign == 'middle':
            self._label.center_y = self.center_y
        else: # 'bottom'
            self._label.y = self.y
コード例 #27
0
ファイル: graphics.py プロジェクト: qbeightol/cs1110
class GObject(Widget):
    """Base graphics object for a `GameView` class.
    
    You should never make a GObject directly.  Instead, you should use one of the
    subclasses: GRectangle, GEllipse, GLine, GImage, and GLabel."""
    # Fields.  See the associated property.
    _fillcolor = colormodel.RGB(0,0,0,1)  # fill color field
    _linecolor = colormodel.RGB(0,0,0,1)  # line color field

    # Kivy properties.  For integration with graphics.kv
    _kivy_fill_color = ListProperty([0,0,0,1]) # Kivy representation of fill color
    _kivy_line_color = ListProperty([0,0,0,1]) # Kivy representation of line color
    
    @property
    def fillcolor(self):
        """The object fill color.
        
        Used to color the backgrounds or, in the case of solid shapes, the shape
        interior.
        
        **Invariant**: Must be an RGB or HSV object from module `colormodel`."""
        return self._fillcolor
    
    @fillcolor.setter
    def fillcolor(self,value):
        assert type(value) in (colormodel.RGB, colormodel.HSV), `value`+' is not a valid color'
        self._fillcolor = value
        self._kivy_fill_color = value.glColor()
        
    @property
    def linecolor(self):
        """The object line color.
        
        Used to color the foreground, text, or, in the case of solid shapes, the
        shape border.
        
        **Invariant**: Must be an RGB or HSV object from module `colormodel`."""
        return self._linecolor
    
    @linecolor.setter
    def linecolor(self,value):
        assert type(value) in (colormodel.RGB, colormodel.HSV), `value`+' is not a valid color'
        self._linecolor = value
        self._kivy_line_color = value.glColor()

    def __init__(self,**keywords):
        """**Constructor**: creates a new graphics object.
        
            :param keywords: dictionary of keyword arguments 
            **Precondition**: See below.
        
        To use the constructor for this class, you should provide
        it with a list of keyword arguments that initialize various
        attributes.  For example, to initialize the x position and
        the fill color, use the constructor call
        
            GObject(x=2,fillcolor=colormodel.RED)
        
        You do not need to provide the keywords as a dictionary.
        The ** in the parameter `keywords` does that automatically.
        
        Any attribute of this class may be used as a keyword.  The
        argument must satisfy the invariants of that attribute.  See
        the list of attributes of this class for more information."""
        super(GObject,self).__init__(**keywords)
        if 'fillcolor' in keywords:
            self.fillcolor = keywords['fillcolor']
        if 'linecolor' in keywords:
            self.linecolor = keywords['linecolor']
        self.size_hint = (None,None)
    

    def collide_widget(w):
        # Prevent students from using this built-in method
        pass