def test_additionalInfo(self):
     mySwitch = Switch()
    
     @mySwitch.case(1)
     def gotOne(value, *args, **kwargs):
         return ('gotOne', value, args, kwargs)
     
     self.assertEqual(mySwitch.switch(1, 'foo', bar=True), ('gotOne', 1, ('foo',), {'bar': True}))
 def test_in_str(self):
     mySwitch = Switch()
     
     @mySwitch.caseIn('lol')
     def gotLOL(value):
         return ('gotLOL', value)
     
     self.assertEqual(mySwitch.switch('frololic'), ('gotLOL', 'lol'))
 def test_defaultHandler(self):
     mySwitch = Switch()
     
     @mySwitch.default
     def gotDefault(value):
         return ('gotDefault', value)
     
     self.assertEqual(mySwitch.switch(1), ('gotDefault', 1))
Ejemplo n.º 4
0
def getHSL(r, g, b):
    # sources:
    # https://stackoverflow.com/questions/39118528/rgb-to-hsl-conversion
    # https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/

    # get base hue value
    r /= 255
    g /= 255
    b /= 255
    maximum = max([r, g, b])
    minimum = min([r, g, b])
    chroma = maximum - minimum
    if chroma == 0:
        hue = 0
    else:
        s = Switch(maximum)
        if s.case(r):
            segment = (g - b) / chroma
            shift = 0
            if segment < 0:
                shift = 6
        if s.case(g):
            segment = (b - r) / chroma
            shift = 2
        if s.case(b):
            segment = (r - g) / chroma
            shift = 4
        hue = (segment + shift) * 60

    # get base luminace value
    luminace = (minimum + maximum) / 2

    # get base saturation value
    if maximum == minimum:
        saturation = 0
    else:
        if luminace < 0.5:
            saturation = chroma / (2 * luminace)
        else:
            saturation = chroma / (2 * (1 - luminace))
        saturation *= 100

    # reformat values
    luminace *= 100
    if hue % 1 == 0:
        hue = int(hue)
    else:
        hue = round(hue, 2)
    if saturation % 1 == 0:
        saturation = int(saturation)
    else:
        saturation = round(saturation, 3)
    if luminace % 1 == 0:
        luminace = int(luminace)
    else:
        luminace = round(luminace, 3)
    return "HSL(" + str(hue) + "," + str(saturation) + "%," + str(
        luminace) + "%)"
 def test_in_sequence(self):
     mySwitch = Switch()
     
     @mySwitch.caseIn('lol')
     def gotLOL(value):
         return ('gotLOL', value)
     
     self.assertEqual(mySwitch.switch(['foo', 'lol', 'bar']),
                      ('gotLOL', 'lol'))
 def test_unknown(self):
     mySwitch = Switch()
     
     with self.assertRaises(SwitchError) as ec:
         mySwitch.switch(1)
     self.assertIn('1', ec.exception.message)
     
     with self.assertRaises(SwitchError) as ec:
         mySwitch.switch('oops')
     self.assertIn('oops', ec.exception.message)
 def test_exact(self):
     mySwitch = Switch()
     
     @mySwitch.case(1)
     def got1(value):
         return ('got1', value)
     
     @mySwitch.case(2)
     def got2(value):
         return ('got2', value)
     
     @mySwitch.case('three')
     def gotThree(value):
         return ('gotThree', value)
     
     @mySwitch.case(4)
     def got4(value):
         return ('got4', value)
         
     self.assertEqual(mySwitch.switch(1), ('got1', 1))
     self.assertEqual(mySwitch.switch(2), ('got2', 2))
     self.assertEqual(mySwitch.switch('three'), ('gotThree', 'three'))
     self.assertEqual(mySwitch.switch(4), ('got4', 4))
     with self.assertRaises(SwitchError):
         mySwitch.switch(0)
         mySwitch.switch(3)
         mySwitch.switch(5)
 def test_regex_single(self):
     mySwitch = Switch()
     
     @mySwitch.caseRegEx('th.*')
     def gotA(matchObj):
         return ('gotA', matchObj.group(0))
     
     @mySwitch.caseRegEx(re.compile('bar$'))
     def gotB(matchObj):
         return ('gotB', matchObj.group(0))
     
     self.assertEqual(mySwitch.switch('thesis'), ('gotA', 'thesis'))
     self.assertEqual(mySwitch.switch('foobar'), ('gotB', 'bar'))
     
     with self.assertRaises(SwitchError):
         mySwitch.switch('barfoo')
 def test_exact_sequence(self):
     mySwitch = Switch()
     
     @mySwitch.case([1, 2, 3])
     def got1thru3(value):
         return ('got1thru3', value)
     
     @mySwitch.case(4)
     def got4(value):
         return ('got4', value)
     
     @mySwitch.case([6, 7])
     def got6thru7(value):
         return ('got6thru7', value)
     
     self.assertEqual(mySwitch.switch(1), ('got1thru3', 1))
     self.assertEqual(mySwitch.switch(2), ('got1thru3', 2))
     self.assertEqual(mySwitch.switch(3), ('got1thru3', 3))
     self.assertEqual(mySwitch.switch(4), ('got4', 4))
     self.assertEqual(mySwitch.switch(6), ('got6thru7', 6))
     self.assertEqual(mySwitch.switch(7), ('got6thru7', 7))
     with self.assertRaises(SwitchError):
         mySwitch.switch(0)
         mySwitch.switch(5)
         myswitch.switch(8)
 def test_mixed(self):
     mySwitch = Switch()
     
     @mySwitch.default
     def gotDefault(value, *args, **kwargs):
         return ('gotDefault', value, args, kwargs)
      
     @mySwitch.case(1)
     def got1(value, *args, **kwargs):
         return ('got1', value, args, kwargs)
     
     @mySwitch.caseIn('hell')
     def getHell(value, *args, **kwargs):
         return ('gotHell', value, args, kwargs)
     
     @mySwitch.case(range(10, 15))
     def gotRange10to15(value, *args, **kwargs):
         return ('gotRange10to15', value, args, kwargs)
     
     @mySwitch.case([ 99, 'hundred' ])
     def gotStuff(value, *args, **kwargs):
         return ('gotStuff', value, args, kwargs)
     
     @mySwitch.caseRegEx('ten.*')
     def gotTenDotStar(matchObj, *args, **kwargs):
         return ('gotTenDotStar', matchObj.group(0), args, kwargs)
     
     self.assertEqual(mySwitch.switch(1, 1.5), ('got1', 1, (1.5,), {}))
     self.assertEqual(mySwitch.switch(11, key='hi'), ('gotRange10to15', 11, (), {'key': 'hi'}))
     self.assertEqual(mySwitch.switch(5, 'oops'), ('gotDefault', 5, ('oops',), {}))
     self.assertEqual(mySwitch.switch('tennison', profession='poet'), ('gotTenDotStar', 'tennison', (), {'profession': 'poet'}))
     self.assertEqual(mySwitch.switch('well, hello there'), ('gotHell', 'hell', (), {}))
Ejemplo n.º 11
0
def getHex(num):
    # assumes an integer between 0 and 255
    hexString = ''

    # get first digit
    s = Switch(math.floor(num / 16))
    if s.case(10):
        hexString = hexString + 'A'
    if s.case(11):
        hexString = hexString + 'B'
    if s.case(12):
        hexString = hexString + 'C'
    if s.case(13):
        hexString = hexString + 'D'
    if s.case(14):
        hexString = hexString + 'E'
    if s.case(15):
        hexString = hexString + 'F'
    if s.default():
        hexString = hexString + str(math.floor(num / 16))

    # get second digit
    s.restart(num % 16)
    if s.case(10):
        hexString = hexString + 'A'
    if s.case(11):
        hexString = hexString + 'B'
    if s.case(12):
        hexString = hexString + 'C'
    if s.case(13):
        hexString = hexString + 'D'
    if s.case(14):
        hexString = hexString + 'E'
    if s.case(15):
        hexString = hexString + 'F'
    if s.default():
        hexString = hexString + str(num % 16)
    return hexString