Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
def test_contrast_rgb():
    """
    Test translation function contrast_value
    """
    print('Testing contrast_rgb')
    # Negative contrast
    rgb = introcs.RGB(240, 15, 118)
    hsv = a3.contrast_rgb(rgb,-0.4)
    introcs.assert_equals(220, rgb.red)
    introcs.assert_equals(35,  rgb.green)
    introcs.assert_equals(123, rgb.blue)
    # Add two more tests
    # zero contrast
    rgb = introcs.RGB(240, 15, 118)
    hsv = a3.contrast_rgb(rgb,0.0)
    introcs.assert_equals(240, rgb.red)
    introcs.assert_equals(15,  rgb.green)
    introcs.assert_equals(118, rgb.blue)
    # positive contrast
    rgb = introcs.RGB(240, 15, 118)
    hsv = a3.contrast_rgb(rgb,0.5)
    introcs.assert_equals(250, rgb.red)
    introcs.assert_equals(5,  rgb.green)
    introcs.assert_equals(99, rgb.blue)
    print('Tests for contrast_rgb passed')
Ejemplo n.º 3
0
def test_rgb_to_cmyk():
    """
    Test translation function rgb_to_cmyk
    """
    print('Testing rgb_to_cmyk')
    # The function should guarantee accuracy to three decimal places
    rgb = introcs.RGB(255, 255, 255);
    cmyk = a3.rgb_to_cmyk(rgb);
    introcs.assert_equals(0.0, round(cmyk.cyan,3))
    introcs.assert_equals(0.0, round(cmyk.magenta,3))
    introcs.assert_equals(0.0, round(cmyk.yellow,3))
    introcs.assert_equals(0.0, round(cmyk.black,3))
    #testing all 0s
    rgb = introcs.RGB(0, 0, 0);
    cmyk = a3.rgb_to_cmyk(rgb);
    introcs.assert_equals(0.0, round(cmyk.cyan,3))
    introcs.assert_equals(0.0, round(cmyk.magenta,3))
    introcs.assert_equals(0.0, round(cmyk.yellow,3))
    introcs.assert_equals(100.0, round(cmyk.black,3))
    #testing normal color
    rgb = introcs.RGB(217, 43, 164);
    cmyk = a3.rgb_to_cmyk(rgb);
    introcs.assert_equals(0.0, round(cmyk.cyan,3))
    introcs.assert_equals(80.184, round(cmyk.magenta,3))
    introcs.assert_equals(24.424, round(cmyk.yellow,3))
    introcs.assert_equals(14.902, round(cmyk.black,3))
    print('Tests for rgb_to_cmyk passed')
Ejemplo n.º 4
0
def test_complement():
    """
    Test function complement
    """
    introcs.assert_equals(introcs.RGB(255-250, 255-0, 255-71),
                          a2.complement_rgb(introcs.RGB(250, 0, 71)))
    introcs.assert_equals(introcs.RGB(255-92, 255-128, 255-255),
                          a2.complement_rgb(introcs.RGB(92, 128, 255)))

    # Make sure we are not modifying the color
    rgb = introcs.RGB(128,128,128)
    introcs.assert_not_equals(id(rgb),id(a2.complement_rgb(rgb)))
Ejemplo n.º 5
0
def test_complement():
    """
    Test function complement
    """
    print('Testing complement')
    # One test is really good enough here
    comp = a3.complement_rgb(introcs.RGB(250, 0, 71))
    introcs.assert_equals(255-250, comp.red)
    introcs.assert_equals(255-0,   comp.green)
    introcs.assert_equals(255-71,  comp.blue)
    # One more for good measure
    comp = a3.complement_rgb(introcs.RGB(128, 64, 255))
    introcs.assert_equals(255-128, comp.red)
    introcs.assert_equals(255-64,  comp.green)
    introcs.assert_equals(255-255, comp.blue)
    print('Test for complement passed')
Ejemplo n.º 6
0
def complement_rgb(rgb):
    """
    Returns: the complement of color rgb.

    Parameter rgb: the color to complement
    Precondition: rgb is an RGB object
    """
    return introcs.RGB(255-rgb.red, 255-rgb.green, 255-rgb.blue)
Ejemplo n.º 7
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 introcs.RGB(rgb.red, rgb.green, rgb.blue)
Ejemplo n.º 8
0
def read_image(file):
    """
    Returns an in-memory image buffer for the given file.
    
    An image buffer is a 2d table of RGB objects.  This is different than the way
    images are represented by the PIL module (which is designed for speed), but it
    is easier for beginners.
    
    This function prints out a simple progress bar to indicate how far along it
    is in loading.  The progress bar consists of several periods followed by 'done'.
    
    If the file does not exist, or there is an error in reading the file, then
    this function returns None.
    
    Paramater file: The image file to read
    Precondition: file is a string
    """
    try:
        image = CoreImage.open(file)
        print(('Loading ' + repr(file)), end='', flush=True)

        # Extract data from PIL
        image = image.convert("RGBA")
        width = image.size[0]
        height = image.size[1]

        # Poor man's progress bar
        size = width * height
        block = max(size // PROGRESS, 1)

        # This is an iterator.  It allows us to "sync" two sequences in the loop
        source = iter(image.getdata())

        # Convert PIL data to student-friendly format
        buffer = []
        for r in range(height):
            row = []
            for c in range(width):
                # Get next PIL pixel and convert to RGB object
                tups = next(source)
                row.append(introcs.RGB(*tups))

                # Update progress bar every block steps
                if (r * width + c) % block == 0:
                    print('.', end='', flush=True)

            buffer.append(row)

        print('done')
        return buffer
    except:
        # This displays error message even though we are not technically crashing
        traceback.print_exc()
        print('Could not load the file ' + repr(file))
        return None
Ejemplo n.º 9
0
    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()
Ejemplo n.º 10
0
def test_rgb_to_cmyk():
    """
    Test translation function rgb_to_cmyk
    """
    rgb = introcs.RGB(255, 255, 255)
    cmyk = a2.rgb_to_cmyk(rgb)
    introcs.assert_equals('0.000', a2.str5(cmyk.cyan))
    introcs.assert_equals('0.000', a2.str5(cmyk.magenta))
    introcs.assert_equals('0.000', a2.str5(cmyk.yellow))
    introcs.assert_equals('0.000', a2.str5(cmyk.black))

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

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

    rgb = introcs.RGB(0, 165, 39)
    cmyk = a2.rgb_to_cmyk(rgb)
    introcs.assert_equals('100.0', a2.str5(cmyk.cyan))
    introcs.assert_equals('0.000', a2.str5(cmyk.magenta))
    introcs.assert_equals('76.36', a2.str5(cmyk.yellow))
    introcs.assert_equals('35.29', a2.str5(cmyk.black))

    rgb = introcs.RGB(23, 66, 188)
    cmyk = a2.rgb_to_cmyk(rgb)
    introcs.assert_equals('87.77', a2.str5(cmyk.cyan))
    introcs.assert_equals('64.89', a2.str5(cmyk.magenta))
    introcs.assert_equals('0.000', a2.str5(cmyk.yellow))
    introcs.assert_equals('26.27', a2.str5(cmyk.black))
Ejemplo n.º 11
0
 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()
Ejemplo n.º 12
0
def test_rgb_to_hsv():
    """
    Test translation function rgb_to_hsv
    """
    print('Testing rgb_to_hsv')
    # ADD TESTS TO ME
    # if M=m
    rgb = introcs.RGB(0,0,0)
    hsv = a3.rgb_to_hsv(rgb)
    introcs.assert_equals(0.0,round(hsv.hue,3))
    introcs.assert_equals(0.0,round(hsv.saturation,3))
    introcs.assert_equals(0.0,round(hsv.value,3))
    #if M == red and green >= blue
    rgb = introcs.RGB(255,240,150)
    hsv = a3.rgb_to_hsv(rgb)
    introcs.assert_equals(51.429,round(hsv.hue,3))
    introcs.assert_equals(0.412,round(hsv.saturation,3))
    introcs.assert_equals(1.0,round(hsv.value,3))
    #M  == red and green < blue
    rgb = introcs.RGB(255,150,240)
    hsv = a3.rgb_to_hsv(rgb)
    introcs.assert_equals(308.571,round(hsv.hue,3))
    introcs.assert_equals(0.412,round(hsv.saturation,3))
    introcs.assert_equals(1.0,round(hsv.value,3))
    #M == green
    rgb = introcs.RGB(100,255,100)
    hsv = a3.rgb_to_hsv(rgb)
    introcs.assert_equals(120.0,round(hsv.hue,3))
    introcs.assert_equals(0.608,round(hsv.saturation,3))
    introcs.assert_equals(1.0,round(hsv.value,3))
    #M == blue
    rgb = introcs.RGB(100,100,255)
    hsv = a3.rgb_to_hsv(rgb)
    introcs.assert_equals(240.0,round(hsv.hue,3))
    introcs.assert_equals(0.608,round(hsv.saturation,3))
    introcs.assert_equals(1.0,round(hsv.value,3))
    print('Tests for rgb_to_hsv passed')
Ejemplo n.º 13
0
def test_rgb_to_cmyk():
    """
    Test translation function rgb_to_cmyk
    """
    rgb = introcs.RGB(255, 255, 255)
    cmyk = a2.rgb_to_cmyk(rgb)
    introcs.assert_equals('0.000', a2.str5(cmyk.cyan))
    introcs.assert_equals('0.000', a2.str5(cmyk.magenta))
    introcs.assert_equals('0.000', a2.str5(cmyk.yellow))
    introcs.assert_equals('0.000', a2.str5(cmyk.black))

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

    rgb = introcs.RGB(217, 43, 164)
    cmyk = a2.rgb_to_cmyk(rgb)
    introcs.assert_equals('0.000', a2.str5(cmyk.cyan))
    introcs.assert_equals('80.18', a2.str5(cmyk.magenta))
    introcs.assert_equals('24.42', a2.str5(cmyk.yellow))
    introcs.assert_equals('14.90', a2.str5(cmyk.black))
Ejemplo n.º 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.
    """
    init_hue = hsv.hue
    init_saturation = hsv.saturation
    init_value = hsv.value
    Hi = math.floor(init_hue/60)
    f = init_hue/60 - Hi
    p = init_value * (1- init_saturation)
    q = init_value * (1- f * init_saturation)
    t = init_value * (1- (1-f) * init_saturation)

    if Hi == 0:
        R = init_value
        G = t
        B = p
    elif Hi == 1:
        R = q
        G = init_value
        B = p
    elif Hi == 2:
        R = p
        G = init_value
        B = t
    elif Hi == 3:
        R = p
        G = q
        B = init_value
    elif Hi == 4:
        R = t
        G = p
        B = init_value
    elif Hi == 5:
        R = init_value
        G = p
        B = q
   
    rgb = introcs.RGB(int(str5(R*255)), int(str5(G*255)), int(str5(B*255)))
   
    return rgb
Ejemplo n.º 15
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 = 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()
Ejemplo n.º 16
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
    assert (type(rgb) == introcs.RGB), rgb + ' is not a RGB object'
    r = rgb.red
    g = rgb.green
    b = rgb.blue
    r = 255 - r
    g = 255 - g
    b = 255 - b
    rgb.red = r
    rgb.green = g
    rgb.blue = b
    return introcs.RGB(rgb.red, rgb.green, rgb.blue)
Ejemplo n.º 17
0
def apply_matrix(matrix, rgb):
    """
    Returns: a new color object resulting from applying matrix to color rgb

    The matrix is applied as follows:
    * The coefficients in the first row are applied to the color to get the red value
    * The coefficients in the second row are applied to the color to get the green value
    * The coefficients in the third row are applied to the color to get the blue value

    Parameter matrix: The colorblind conversion matrix
    Precondition: matrix is a 3x3 matrix, which each row summing to 1.0

    Parameter rgb: the color to convert to a CMYK object
    Precondition: rgb is an RGB object
    """
    new_r = components_to_num(matrix[0], rgb)
    new_g = components_to_num(matrix[1], rgb)
    new_b = components_to_num(matrix[2], rgb)
    return introcs.RGB(new_r, new_g, new_b)
Ejemplo n.º 18
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.
    """
    assert (type(cmyk) == introcs.CMYK), cmyk + ' is not an CMYK object'
    # The CMYK numbers are in the range 0.0..100.0.
    # Deal with them the same way as the RGB numbers in rgb_to_cmyk()
    cyan = cmyk.cyan / 100.0
    magenta = cmyk.magenta / 100.0
    yellow = cmyk.yellow / 100.0
    black = cmyk.black / 100.0
    red = round((1 - cyan) * (1 - black) * 255)
    green = round((1 - magenta) * (1 - black) * 255)
    blue = round((1 - yellow) * (1 - black) * 255)
    return introcs.RGB(red, green, blue)
Ejemplo n.º 19
0
def hsv_to_rgb(hsv):
    """
    Returns an RGB object equivalent to hsv

    Formulae from https://en.wikipedia.org/wiki/HSL_and_HSV

    Parameter hsv: the color to convert to a RGB object
    Precondition: hsv is an HSV object.
    """
    assert (type(hsv) == introcs.HSV), hsv + ' is not a RGB object'
    hi = math.floor(hsv.hue / 60)
    f = hsv.hue / 60 - hi
    p = hsv.value * (1 - hsv.saturation)
    q = hsv.value * (1 - f * hsv.saturation)
    t = hsv.value * (1 - (1 - f) * hsv.saturation)
    if hi == 0:
        red = hsv.value
        green = t
        blue = p
    elif hi == 1:
        red = q
        green = hsv.value
        blue = p
    elif hi == 2:
        red = p
        green = hsv.value
        blue = t
    elif hi == 3:
        red = p
        green = q
        blue = hsv.value
    elif hi == 4:
        red = t
        green = p
        blue = hsv.value
    elif hi == 5:
        red = hsv.value
        green = p
        blue = q
    return introcs.RGB(round(red * 255), round(green * 255), round(blue * 255))
Ejemplo n.º 20
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 = (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)

    rgb = introcs.RGB(int(str5(R*255)),int(str5(G*255)),int(str5(B*255)))
    
    return rgb
Ejemplo n.º 21
0
#: the width of the game display
GAME_WIDTH  = 800
#: the height of the game display
GAME_HEIGHT = 700

### SPARK CONSTANTS ###

# Gravity of surface
GRAVITY = -0.2
# For the explosion
PARTICLE_DIAMETER = 5
MAX_INIT_VEL = 5
PARTICLES_PER_SHELL = 20

# Colors
WHITE_COLOR = introcs.RGB(255,255,255)
GRAY_COLOR  = introcs.RGB(128,128,128)

### HEART CONSTANTS ###

# the width of the heart
HEART_WIDTH = 50
# the height of the heart
HEART_HEIGHT = 50

### POWERUP CONSTANTS ###

# the width of a powerup
PUP_WIDTH = 50
#the height of a powerup
PUP_HIEGHT = 50
Ejemplo n.º 22
0
### ADD MORE CONSTANTS (PROPERLY COMMENTED) AS NECESSARY ###

####### New Variables for drawing aliens #######

# Distance from the left end of the game to first alien
LEFT_TO_FIRST = ALIEN_H_SEP + ALIEN_WIDTH/2

# Horizontal distance from alien to next alien 
ADDING_ROW = ALIEN_WIDTH + ALIEN_H_SEP

# Distance from top end of the game to first alien
TOP_TO_FIRST = GAME_HEIGHT - (ALIEN_CEILING + ALIEN_WIDTH/2)

# Vertical distance from alien to next alien 
ADDING_COLUMN = ALIEN_V_SEP + ALIEN_HEIGHT

####### Variable for defensive line #######

# Color of the defensive line
COLOR = introcs.RGB(0,0,0)

####### Color Variables #######

TEXT_COLOR = introcs.HSV(0.2, 0.3, 0.4)

LINE_COLOR = introcs.HSV(0.5, 1.0, 0.3)

####### Distance Variable #######

BORDER_RIGHT = GAME_WIDTH - ALIEN_WIDTH/2 - ALIEN_H_SEP