# a3.py # YOUR NAME(S) AND NETID(S) HERE # DATE COMPLETED HERE """ Functions for Assignment A3""" import colormodel import math 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' # THIS IS WRONG. FIX IT return colormodel.RGB(rgb.red, rgb.green, rgb.blue) def truncate5(value): """Returns: value, as a string, using exactly 5 characters. The truncated value will have one of the forms: ddd.d Example: '360.1' dd.dd Example: '29.53' d.ddd Examples: '4.003', '0.001', and '0.000' Precondition: value is a number (int or float), 0 <= value <= 999.""" assert (type(value) == int or type(value) == float), 'Value '+ `value`+' is not a number' assert (0 <= value and value <= 999), 'Value '+ `value`+' is outside of the range 0..999' # To get the desired output, do the following # 1. Make sure value is a float. If it is not, convert it to one. # 1. If value < 0.001, set value to 0. # This prevents value appearing in scientific notation, e.g. 1.5E-6. # 2. Convert value to a string s, in the usual way. Note that s is guaranteed to # have at least three chars: a decimal point and a digit on either side of it. # Therefore, the simplest thing to do as s is being constructed is to make # sure s has at least 5 chars by appending "00" after the decimal point. # 3. Return the first five characters of s. return '' def round5(value): """ Returns: value, as a string, but expanded or rounded to be (if necessary) exactly 5 characters. Examples: Round 1.3546 to '1.355'. Round 1.3544 to '1.354'. Round 21.9954 to '22.00'. Round 21.994 to '21.99'. Round 130.59 to '130.6'. Round 130.54 to '130.5'. Round 1 to '1.000'. Precondition: value is a number (int or float), 0 <= value <= 360.""" assert (type(value) == int or type(value) == float), 'Value '+ `value`+' is not a number' assert (0 <= value and value <= 360), 'Value '+ `value`+' is outside of the range 0..360.' # Note. You want to round and then call truncate5. DO NOT convert value # to a string and then back to a float in order to call truncate5. # Points will be deducted for this. # # MAKE SURE THAT VALUE IS A FLOAT BEFORE USING THE BUILT-IN round() FUNCTION # If it is not a float convert it to one with the float() function. # # Obviously, you want to use the built-in function round(). However, # remember that the rounding takes place at a different place depending on # how big value is. Look at the examples in the specification. # ASSERT PRECONDITIONS return '' def rgb_to_string(rgb): """Returns: String representation of rgb in the form "(R, G, B)". Precondition: rgb is an RGB object""" # ASSERT PRECONDITIONS return '' def cmyk_to_string(cmyk): """Returns: String representation of cmyk in the form "(C, M, Y, K)". In the output, each of C, M, Y, and K should be exactly 5 characters long. Precondition: cmyk is an CMYK object.""" # ASSERT PRECONDITIONS return '' def hsv_to_string(hsv): """Returns: String representation of hsv in the form "(H, S, V)". In the output, each of H, S, and V should be exactly 5 characters long. Precondition: hsv is an HSV object.""" # ASSERT PRECONDITIONS return '' def rgb_to_cmyk(rgb): """Returns: color rgb as a CMYK object, with the most black possible. Formulae from en.wikipedia.org/wiki/CMYK_color_model. Precondition: rgb is an RGB object""" # ASSERT PRECONDITIONS return None def cmyk_to_rgb(cmyk): """Returns : color CMYK in as an RGB object. Formulae from en.wikipedia.org/wiki/CMYK_color_model. Precondition: cmyk is an CMYK object.""" # ASSERT PRECONDITIONS return None def rgb_to_hsv(rgb): """Return: color rgb as a HSV object. Formulae from wikipedia.org/wiki/HSV_color_space. Precondition: rgb is an RGB object""" # ASSERT PRECONDITIONS return None def hsv_to_rgb(hsv): """Returns: color hsv as an RGB object. Formulae from http://en.wikipedia.org/wiki/HSV_color_space. Precondition: hsv is an HSV object.""" # ASSERT PRECONDITIONS return None