Beispiel #1
0
def test_rgb_to_cmyk():
    """
    Test translation function rgb_to_cmyk
    """
    # We use a3.str5 to handle round-off error in comparisons
    rgb = cornell.RGB(255, 255, 255)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornell.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornell.assert_equals('0.000', a3.str5(cmyk.magenta))
    cornell.assert_equals('0.000', a3.str5(cmyk.yellow))
    cornell.assert_equals('0.000', a3.str5(cmyk.black))

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

    rgb = cornell.RGB(217, 43, 164)
    cmyk = a3.rgb_to_cmyk(rgb)
    cornell.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornell.assert_equals('80.18', a3.str5(cmyk.magenta))
    cornell.assert_equals('24.42', a3.str5(cmyk.yellow))
    cornell.assert_equals('14.90', a3.str5(cmyk.black))
Beispiel #2
0
    def __init__(self):
        """
        Initializes the Background.

        This method calls super() to use the initializer of GRectangle to create the
        image of the background. Sets the width as GAME_WIDTH, height as GAME_HEIGHT,
        x as GAME_WIDTH/2, y as GAME_HEIGHT/2, the fillcolor as a dark grey rgb object,
        and the linecolor as a dark grey rgb object.
        """
        super().__init__(x=GAME_WIDTH / 2,
                         y=GAME_HEIGHT / 2,
                         width=GAME_WIDTH,
                         height=GAME_HEIGHT,
                         fillcolor=cornell.RGB(50, 50, 50),
                         linecolor=cornell.RGB(50, 50, 50))
Beispiel #3
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()
    
    c_new = cmyk.cyan/100.0
    m_new = cmyk.magenta/100.0
    y_new = cmyk.yellow/100.0
    k_new = cmyk.black/100.0
    
    r_final = (1-c_new)*(1 - k_new)
    g_final = (1-m_new)*(1-k_new)
    b_final = (1-y_new)*(1-k_new)
    
    r_finale = int(round((r_final*255),0))
    g_finale = int(round((g_final*255),0))
    b_finale = int(round((b_final*255),0))
    
    return cornell.RGB(r_finale, g_finale, b_finale)
Beispiel #4
0
 def WHEN_STATE_COMPLETE(self, value):
     """ The GLabel to display when the state if the state is complete """
     if value == 0:
         return GLabel(text='The Aliens have invaded!',
                       x=(GAME_WIDTH / 2.0),
                       y=(GAME_HEIGHT / 2.0),
                       font_size=60,
                       font_name="Arcade.ttf",
                       linecolor=cornell.RGB(0, 255, 0))
     else:
         return GLabel(text='Aliens have been defeated!',
                       x=(GAME_WIDTH / 2.0),
                       y=(GAME_HEIGHT / 2.0),
                       font_size=60,
                       font_name="Arcade.ttf",
                       linecolor=cornell.RGB(0, 255, 0))
Beispiel #5
0
 def WHEN_STATE_PAUSED(self):
     """The GLabels that displays when the game is paused"""
     return GLabel(text='Press spacebar to continue',
                   x=(GAME_WIDTH / 2.0),
                   y=(GAME_HEIGHT / 2.0),
                   font_size=60,
                   font_name="Arcade.ttf",
                   fillcolor=cornell.RGB(255, 0, 0))
Beispiel #6
0
 def CREDITS(self):
     """ One of the GLabels to display when the state if the state is inactive """
     return GLabel(text='By Hamed Rabah and Erick Rocha',
                   x=(GAME_WIDTH / 2.0),
                   y=(GAME_HEIGHT / 2.5),
                   font_size=30,
                   font_name="Arcade.ttf",
                   linecolor=cornell.RGB(255, 255, 255))
Beispiel #7
0
 def Ships_Killed(self):
     """A GLabels that displays how many player lives are remaining"""
     return GLabel(text='Lives: ' + str(self._wave.getLives()),
                   x=(0 + 90.0),
                   y=(GAME_HEIGHT - 40.0),
                   font_size=30,
                   font_name="Arcade.ttf",
                   linecolor=cornell.RGB(255, 255, 255))
Beispiel #8
0
 def Aliens_Killed(self):
     """A GLabels that displays the points earned from killing aliens"""
     return GLabel(text='Score: ' + str(self._wave.getAliensKilled()),
                   x=(GAME_WIDTH - 90.0),
                   y=(GAME_HEIGHT - 40.0),
                   font_size=30,
                   font_name="Arcade.ttf",
                   linecolor=cornell.RGB(255, 255, 0))
Beispiel #9
0
 def WHEN_STATE_INACTIVE(self):
     """ One of the GLabels to display when the state if the state is inactive """
     return GLabel(text='PYTHON INVADERZ',
                   x=(GAME_WIDTH / 2.0),
                   y=(GAME_HEIGHT / 2.0),
                   font_size=60,
                   font_name="Arcade.ttf",
                   linecolor=cornell.RGB(0, 255, 0))
Beispiel #10
0
 def INSTRUCTIONS(self):
     """ One of the GLabels to display when the state if the state is inactive """
     return GLabel(
         text='Use the arrow pad to move. Press spacebar to play.',
         x=(GAME_WIDTH / 2.0),
         y=(GAME_HEIGHT / 4.0),
         font_size=30,
         font_name="Arcade.ttf",
         linecolor=cornell.RGB(255, 255, 255))
Beispiel #11
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 cornell.RGB(255 - rgb.red, 255 - rgb.green, 255 - rgb.blue)
Beispiel #12
0
def test_rgb_to_hsv():
    """
    Test translation function rgb_to_hsv
    """
    # Testing MAX == MIN
    rgb = cornell.RGB(0, 0, 0)
    hsv = a3.rgb_to_hsv(rgb)
    cornell.assert_equals('0.000', a3.str5(hsv.hue))
    cornell.assert_equals('0.000', a3.str5(hsv.saturation))
    cornell.assert_equals('0.000', a3.str5(hsv.value))
    cornell.assert_equals('(0.000, 0.000, 0.000)', a3.str5_hsv(hsv))

    # Testing MAX == R and G >= B
    rgb = cornell.RGB(255, 150, 50)
    hsv = a3.rgb_to_hsv(rgb)
    cornell.assert_equals('29.27', a3.str5(hsv.hue))
    cornell.assert_equals('0.804', a3.str5(hsv.saturation))
    cornell.assert_equals('1.000', a3.str5(hsv.value))
    cornell.assert_equals('(29.27, 0.804, 1.000)', a3.str5_hsv(hsv))

    # Test MAX == R and G < B
    rgb = cornell.RGB(255, 50, 150)
    hsv = a3.rgb_to_hsv(rgb)
    cornell.assert_equals('330.7', a3.str5(hsv.hue))
    cornell.assert_equals('0.804', a3.str5(hsv.saturation))
    cornell.assert_equals('1.000', a3.str5(hsv.value))
    cornell.assert_equals('(330.7, 0.804, 1.000)', a3.str5_hsv(hsv))

    # Test MAX == G
    rgb = cornell.RGB(100, 240, 20)
    hsv = a3.rgb_to_hsv(rgb)
    cornell.assert_equals('98.18', a3.str5(hsv.hue))
    cornell.assert_equals('0.917', a3.str5(hsv.saturation))
    cornell.assert_equals('0.941', a3.str5(hsv.value))
    cornell.assert_equals('(98.18, 0.917, 0.941)', a3.str5_hsv(hsv))

    # Test MAX == B
    rgb = cornell.RGB(100, 20, 240)
    hsv = a3.rgb_to_hsv(rgb)
    cornell.assert_equals('261.8', a3.str5(hsv.hue))
    cornell.assert_equals('0.917', a3.str5(hsv.saturation))
    cornell.assert_equals('0.941', a3.str5(hsv.value))
    cornell.assert_equals('(261.8, 0.917, 0.941)', a3.str5_hsv(hsv))
Beispiel #13
0
def test_complement():
    """
    Test function complement
    """
    cornell.assert_equals(cornell.RGB(255 - 250, 255 - 0, 255 - 71),
                          a3.complement_rgb(cornell.RGB(250, 0, 71)))
    cornell.assert_equals(cornell.RGB(255 - 0, 255 - 0, 255 - 0),
                          a3.complement_rgb(cornell.RGB(0, 0, 0)))
    cornell.assert_equals(cornell.RGB(255 - 255, 255 - 255, 255 - 255),
                          a3.complement_rgb(cornell.RGB(255, 255, 255)))
    cornell.assert_equals(cornell.RGB(255 - 173, 255 - 69, 255 - 4),
                          a3.complement_rgb(cornell.RGB(173, 69, 4)))
Beispiel #14
0
 def _explodeRocket(self, rocket):
     """
     Explodes the ship rocket.
     
     Parameter rocket: The rocket to explode
     Precondition: rocket must be of type Rocket.
     """
     color = cornell.RGB(random.randrange(256), random.randrange(256),
                         random.randrange(256))
     for i in range(PARTICLES_PER_SHELL):
         spark = Spark(rocket.x, rocket.y, color)
         self._sparks.append(spark)
Beispiel #15
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.
    """

    h = hsv.hue
    s = hsv.saturation
    v = hsv.value

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

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

    r = round(r * 255, 0)
    g = round(g * 255, 0)
    b = round(b * 255, 0)

    rgb = cornell.RGB(r, g, b)

    return rgb
Beispiel #16
0
    def on_rgb_press(self, r, g, b):
        """
        Callback to rgb button
        """
        self.rgb = cornell.RGB(r, g, b)
        self.hsv = a3.rgb_to_hsv(self.rgb)
        assert (self.hsv == None or type(self.hsv) == cornell.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) == cornell.CMYK), \
                'rgb_to_cmyk does not return a CMYK object'
        self.update()
Beispiel #17
0
    def register(self):
        """
        Initializes color values and forces refresh
        """
        active = True
        self.rgb = cornell.RGB(0, 255, 0)
        self.cmyk = a3.rgb_to_cmyk(self.rgb)
        assert (self.cmyk == None or type(self.cmyk) == cornell.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) == cornell.HSV), \
                'rgb_to_hsv does not return a HSV object'
        self.update()
Beispiel #18
0
def complement_rgb(rgb):
    """
    Returns: the complement of color rgb.
    
    Parameter rgb: the color to complement
    Precondition: rgb is an RGB object
    """

    complement = cornell.RGB(0, 0, 0)

    complement.red = 255 - rgb.red
    complement.green = 255 - rgb.green
    complement.blue = 255 - rgb.blue

    return complement
Beispiel #19
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.
    """
    H_i = math.floor(hsv.hue/60)
    f = (hsv.hue/60) - H_i
    p = hsv.value*(1-hsv.saturation)
    q = hsv.value*(1-(f*hsv.saturation))
    t = hsv.value*(1-(1-f)*hsv.saturation)
    
    if H_i == 0:
        R = hsv.value
        G = t
        B = p
    elif H_i == 1:
        R = q
        G = hsv.value
        B = p
    elif H_i == 2:
        R = p
        G = hsv.value
        B = t
    elif H_i == 3:
        R = p
        G = q
        B = hsv.value
    elif H_i == 4:
        R = t
        G = p
        B = hsv.value
    elif H_i == 5:
        R = hsv.value
        G = p
        B = q
        
    r_finale = int(round(R*255,0))
    g_finale = int(round(G*255,0))
    b_finale = int(round(B*255,0))
    
    return cornell.RGB(r_finale, g_finale, b_finale)
Beispiel #20
0
    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 = cornell.RGB(red, green, blue)
        self.hsv = a3.rgb_to_hsv(self.rgb)
        assert (self.hsv == None or type(self.hsv) == cornell.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) == cornell.CMYK), \
                'rgb_to_cmyk does not return a CMYK object'
        self.update()
Beispiel #21
0
def test_rgb_to_hsv():
    """
    Test translation function rgb_to_hsv
    """
    rgb = cornell.RGB(0, 0, 0);
    hsv = a3.rgb_to_hsv(rgb);
    cornell.assert_equals('0.000', a3.str5(hsv.hue))
    cornell.assert_equals('0.000', a3.str5(hsv.saturation))
    cornell.assert_equals('0.000', a3.str5(hsv.value))

    rgb = cornell.RGB(150, 10, 5);
    hsv = a3.rgb_to_hsv(rgb);
    cornell.assert_equals('2.069', a3.str5(hsv.hue))
    cornell.assert_equals('0.967', a3.str5(hsv.saturation))
    cornell.assert_equals('0.588', a3.str5(hsv.value))
    
    rgb = cornell.RGB(150, 10, 10);
    hsv = a3.rgb_to_hsv(rgb);
    cornell.assert_equals('0.000', a3.str5(hsv.hue))
    cornell.assert_equals('0.933', a3.str5(hsv.saturation))
    cornell.assert_equals('0.588', a3.str5(hsv.value))
    
    rgb = cornell.RGB(150, 60, 10);
    hsv = a3.rgb_to_hsv(rgb);
    cornell.assert_equals('21.43', a3.str5(hsv.hue))
    cornell.assert_equals('0.933', a3.str5(hsv.saturation))
    cornell.assert_equals('0.588', a3.str5(hsv.value))
    
    rgb = cornell.RGB(150, 255, 10);
    hsv = a3.rgb_to_hsv(rgb);
    cornell.assert_equals('85.71', a3.str5(hsv.hue))
    cornell.assert_equals('0.961', a3.str5(hsv.saturation))
    cornell.assert_equals('1.000', a3.str5(hsv.value))
    
    rgb = cornell.RGB(150, 76, 255);
    hsv = a3.rgb_to_hsv(rgb);
    cornell.assert_equals('264.8', a3.str5(hsv.hue))
    cornell.assert_equals('0.702', a3.str5(hsv.saturation))
    cornell.assert_equals('1.000', a3.str5(hsv.value))
    
    rgb = cornell.RGB(255, 255, 255);
    hsv = a3.rgb_to_hsv(rgb);
    cornell.assert_equals('0.000', a3.str5(hsv.hue))
    cornell.assert_equals('0.000', a3.str5(hsv.saturation))
    cornell.assert_equals('1.000', a3.str5(hsv.value))
Beispiel #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()

    # Initial Values/Variables
    C = cmyk.cyan
    M = cmyk.magenta
    Y = cmyk.yellow
    K = cmyk.black

    #Changing the range of the CMYK Numbers to 0.0 ..1.0
    C_1 = C / 100
    M_1 = M / 100
    Y_1 = Y / 100
    K_1 = K / 100

    #Conversion to RGB values
    R = (1 - C_1) * (1 - K_1)
    G = (1 - M_1) * (1 - K_1)
    B = (1 - Y_1) * (1 - K_1)

    # Changing range of RGB to 0 ..255, Rounding involved
    R_1 = round(R * 255, 0)
    G_1 = round(G * 255, 0)
    B_1 = round(B * 255, 0)

    #Converting to int
    R_2 = int(R_1)
    G_2 = int(G_1)
    B_2 = int(B_1)

    #Return new RGB object
    return cornell.RGB(R_2, G_2, B_2)
Beispiel #23
0
def test_rgb_to_cmyk():
    """
    Test translation function rgb_to_cmyk
    """
    # We use a3.str5 to handle round-off error in comparisons
    rgb = cornell.RGB(255, 255, 255);
    cmyk = a3.rgb_to_cmyk(rgb);
    cornell.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornell.assert_equals('0.000', a3.str5(cmyk.magenta))
    cornell.assert_equals('0.000', a3.str5(cmyk.yellow))
    cornell.assert_equals('0.000', a3.str5(cmyk.black))
    
    rgb = cornell.RGB(0, 0, 0);
    cmyk = a3.rgb_to_cmyk(rgb);
    cornell.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornell.assert_equals('0.000', a3.str5(cmyk.magenta))
    cornell.assert_equals('0.000', a3.str5(cmyk.yellow))
    cornell.assert_equals('100.0', a3.str5(cmyk.black))
        
    rgb = cornell.RGB(217, 43, 164);
    cmyk = a3.rgb_to_cmyk(rgb);
    cornell.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornell.assert_equals('80.18', a3.str5(cmyk.magenta))
    cornell.assert_equals('24.42', a3.str5(cmyk.yellow))
    cornell.assert_equals('14.90', a3.str5(cmyk.black))
    
    rgb = cornell.RGB(5, 17, 150);
    cmyk = a3.rgb_to_cmyk(rgb);
    cornell.assert_equals('96.67', a3.str5(cmyk.cyan))
    cornell.assert_equals('88.67', a3.str5(cmyk.magenta))
    cornell.assert_equals('0.000', a3.str5(cmyk.yellow))
    cornell.assert_equals('41.18', a3.str5(cmyk.black))
    
    rgb = cornell.RGB(254, 17, 3);
    cmyk = a3.rgb_to_cmyk(rgb);
    cornell.assert_equals('0.000', a3.str5(cmyk.cyan))
    cornell.assert_equals('93.31', a3.str5(cmyk.magenta))
    cornell.assert_equals('98.82', a3.str5(cmyk.yellow))
    cornell.assert_equals('0.392', a3.str5(cmyk.black))
    
    rgb = cornell.RGB(75, 3, 179);
    cmyk = a3.rgb_to_cmyk(rgb);
    cornell.assert_equals('58.10', a3.str5(cmyk.cyan))
    cornell.assert_equals('98.32', a3.str5(cmyk.magenta))
    cornell.assert_equals('0.000', a3.str5(cmyk.yellow))
    cornell.assert_equals('29.80', a3.str5(cmyk.black))
Beispiel #24
0
def test_complement():
    """
    Test function complement
    """
    cornell.assert_equals(cornell.RGB(255 - 250, 255 - 0, 255 - 71),
                          a3.complement_rgb(cornell.RGB(250, 0, 71)))