Exemple #1
0
def test_hsv_to_rgb():
    """
    Test translation function hsv_to_rgb
    """
    hsv = introcs.HSV(178, 0.999, 0.001)
    rgb = a2.hsv_to_rgb(hsv)
    introcs.assert_equals('0.000', a2.str5(rgb.red))
    introcs.assert_equals('0.000', a2.str5(rgb.green))
    introcs.assert_equals('0.000', a2.str5(rgb.blue))

    hsv = introcs.HSV(77.23, 0.652, 0.148)
    rgb = a2.hsv_to_rgb(hsv)
    introcs.assert_equals('38.00', a2.str5(rgb.green))
    introcs.assert_equals('31.00', a2.str5(rgb.red))
    introcs.assert_equals('13.00', a2.str5(rgb.blue))

    hsv = introcs.HSV(0.000, 1.000, 0.765)
    rgb = a2.hsv_to_rgb(hsv)
    introcs.assert_equals('0.000', a2.str5(rgb.green))
    introcs.assert_equals('0.000', a2.str5(rgb.blue))
    introcs.assert_equals('195.0', a2.str5(rgb.red))

    hsv = introcs.HSV(240.1, 0.366, 0.771)
    rgb = a2.hsv_to_rgb(hsv)
    introcs.assert_equals('125.0', a2.str5(rgb.red))
    introcs.assert_equals('125.0', a2.str5(rgb.green))
    introcs.assert_equals('197.0', a2.str5(rgb.blue))

    hsv = introcs.HSV(222.8, 1.000, 0.521)
    rgb = a2.hsv_to_rgb(hsv)
    introcs.assert_equals('0.000', a2.str5(rgb.red))
    introcs.assert_equals('38.00', a2.str5(rgb.green))
    introcs.assert_equals('133.0', a2.str5(rgb.blue))
Exemple #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')
Exemple #3
0
def rgb_to_hsv(rgb):
    """
    Return an HSV object equivalent to rgb

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

    Parameter hsv: the color to convert to a HSV 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 range 0..1 by dividing them by 255.0.
    red = rgb.red / 255.0
    green = rgb.green / 255.0
    blue = rgb.blue / 255.0
    maximum = max(red, green, blue)
    minimum = min(red, green, blue)
    if maximum == minimum:
        hue = 0.0
    elif maximum == red and green >= blue:
        hue = 60.0 * (green - blue) / (maximum - minimum)
    elif maximum == red and green < blue:
        hue = 60.0 * (green - blue) / (maximum - minimum) + 360.0
    elif maximum == green:
        hue = 60.0 * (blue - red) / (maximum - minimum) + 120.0
    else:
        hue = 60.0 * (red - green) / (maximum - minimum) + 240.0
    if maximum == 0:
        saturation = 0
    else:
        saturation = 1 - minimum / maximum
    value = maximum
    return introcs.HSV(hue, saturation, value)
Exemple #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)))
Exemple #5
0
    def on_hsv_press(self,h,s,v):
        """
        Callback to hsv button
        """
        self.hsv = introcs.HSV(h, s, v)
        temp = a2.hsv_to_rgb(self.hsv)
        assert (temp == None or type(temp) == introcs.RGB), \
                'hsv_to_rgb does not return a RGB object'

        self.rgb = self.rgb if temp is None else temp
        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()
Exemple #6
0
    def on_hsv_slide(self,h,s,v):
        """
        Callback to hsv sliders
        """
        if not self.active:
            return
        hue = h / 100.0
        sat = s / 100.0
        val = v / 100.0
        self.hsv = introcs.HSV(hue, sat, val)
        temp = a2.hsv_to_rgb(self.hsv)
        assert (temp == None or type(temp) == introcs.RGB), \
                'hsv_to_rgb does not return a RGB object'

        self.rgb = self.rgb if temp is None else temp
        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()
Exemple #7
0
def rgb_to_hsv(rgb):
    """
    Return: color rgb in HSV color space.

    Formulae from wikipedia.org/wiki/HSV_color_space.

    Parameter rgb: the color to convert to a HSV object
    Precondition: rgb is an RGB object
    """
    # The RGB numbers are in the range 0..255.
    # Change them to range 0..1 by dividing them by 255.0.
    
    r = rgb.red/255.0
    g = rgb.green/255.0
    b = rgb.blue/255.0
    min_comparison =min(r,g,b)
    max_comparison =max(r,g,b)

    value = max_comparison

    if max_comparison == min_comparison:
        hue = 0
    elif max_comparison == r and g >= b:
        hue = (60.0 * (g - b)) / (max_comparison - min_comparison)
    elif max_comparison == r and b >= g:
        hue = (60.0 * (g - b)) / (max_comparison - min_comparison) + 360.0
    elif max_comparison == g:
        hue = (60.0 * (b - r)) / (max_comparison - min_comparison) + 120.0
    else:
        hue = (60.0 * (r - g)) / (max_comparison - min_comparison) + 240.0
   
    if max_comparison == 0:
        saturation = 0
    else:
        saturation = (max_comparison-min_comparison)/max_comparison
      
    hsv = introcs.HSV (hue, saturation, value)
    
    return hsv
Exemple #8
0
def test_hsv_to_rgb():
    """
    Test translation function hsv_to_rgb
    """
    print('Testing hsv_to_rgb')
    # ADD TESTS TO ME
    #Hi == 0
    hsv = introcs.HSV(0.0, 0.0, 0.0);
    rgb = a3.hsv_to_rgb(hsv);
    introcs.assert_equals(0.0,round(rgb.red,3))
    introcs.assert_equals(0.0,round(rgb.green,3))
    introcs.assert_equals(0.0,round(rgb.blue,3))
    #Hi == 1
    hsv = introcs.HSV(70.0, 0.5, 1.0);
    rgb = a3.hsv_to_rgb(hsv);
    introcs.assert_equals(234,round(rgb.red,3))
    introcs.assert_equals(255,round(rgb.green,3))
    introcs.assert_equals(128,round(rgb.blue,3))
    #Hi == 2
    hsv = introcs.HSV(130.0, 0.5, 1.0);
    rgb = a3.hsv_to_rgb(hsv);
    introcs.assert_equals(128,round(rgb.red,3))
    introcs.assert_equals(255,round(rgb.green,3))
    introcs.assert_equals(149,round(rgb.blue,3))
    #Hi == 3
    hsv = introcs.HSV(190.0, 0.5, 1.0);
    rgb = a3.hsv_to_rgb(hsv);
    introcs.assert_equals(128,round(rgb.red,3))
    introcs.assert_equals(234,round(rgb.green,3))
    introcs.assert_equals(255,round(rgb.blue,3))
    #Hi == 4
    hsv = introcs.HSV(250.0, 0.5, 1.0);
    rgb = a3.hsv_to_rgb(hsv);
    introcs.assert_equals(149,round(rgb.red,3))
    introcs.assert_equals(128,round(rgb.green,3))
    introcs.assert_equals(255,round(rgb.blue,3))
    #Hi == 5
    hsv = introcs.HSV(310.0, 0.5, 1.0);
    rgb = a3.hsv_to_rgb(hsv);
    introcs.assert_equals(255,round(rgb.red,3))
    introcs.assert_equals(128,round(rgb.green,3))
    introcs.assert_equals(234,round(rgb.blue,3))
    print('Tests for hsv_to_rgb passed')
Exemple #9
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
Exemple #10
0
def radiate_petals_helper(t, radius, width, n, sp):
    """
    Draws a color flower with n petals of length radius at equal angles.

    The petals alternate between open (a diamond of the given width) and
    closed (a straight line), starting with an open petal.. Open petals are
    drawn with function draw_diamond, while closed petals are drawn by
    moving the turtle in a straight line. After drawing each petal, the
    turtle should return to its original position.

    The petals are drawn at equal angles starting from the initial turtle
    heading. A petal drawn at angle ang, 0 <= ang < 360 has the HSV color
    (ang % 360.0, 1, 1).

    WHEN DONE, THE FOLLOWING TURTLE ATTRIBUTES ARE THE SAME AS IT STARTED:
    color, speed, visible, and drawmode. However, the final position and
    heading may be different. If you changed any of these four in the
    function, you must change them back.

    This procedure asserts all preconditions.

    Parameter t: The drawing Turtle
    Precondition: t is a Turtle with drawmode True.

    Parameter radius: The radius of the produced "flower"
    Precondition: radius is a valid side length (number >= 0)

    Parameter width: The width of an open petal
    Precondition: width is a valid side length (number >= 0)

    Parameter n: The number of lines to draw
    Precondition: n is an int >= 2

    Parameter sp: The turtle speed.
    Precondition: sp is a valid turtle speed.
    """
    # Assert the preconditions
    assert is_valid_turtlemode(t), report_error('Invalid turtle mode', t)
    assert is_valid_length(radius), report_error(
        'radius is not a valid length', radius)
    assert is_valid_length(width), report_error('width is not a valid length',
                                                width)
    assert (type(n) == int
            and n >= 2), report_error('n is an invalid # of petals', n)
    assert is_valid_speed(sp), report_error('sp is not a valid speed', sp)

    # Hints:
    original_x = t.x
    original_y = t.y
    original_heading = t.heading
    originalcolor = t.color
    anglebw = 360 / n
    # 1. Drawing the petals should be drawn with a range loop.
    for i in range(0, n):
        if i % 2 == 0:
            t.color = introcs.HSV(t.heading, 1.0, 1.0)
            t.heading += anglebw
            t.left(anglebw)
            if t.heading > 360:
                t.heading -= 360
            draw_diamond(t, width, 100)

        if i % 2 == 1:
            t.color = introcs.HSV(t.heading, 1.0, 1.0)

            t.heading += anglebw
            t.drawmode = True
            if t.heading > 360:
                t.heading -= 360
            t.forward(radius)
            t.drawmode = False
            t.backward(radius)
        t.color = originalcolor
    # 2. The first petal should be open, alternating with closed petals afterwards
    # 3. Open petals should be drawn with the function draw_diamond
    # 4. The heading of the turtle should stay in the range 0 <= heading < 360.
    # 5. (t.heading % 360.0, 1, 1) is the HSV color of the turtle for each petal
    # 6. You can use an HSV object for the turtle's color attribute,
    #    even though all the examples use strings with color names
    pass