Changeset 2869
- Timestamp:
- 03/02/07 16:28:35 (2 years ago)
- Files:
-
- trunk/dabo/dColors.py (modified) (6 diffs)
- trunk/tests/unitTests/Test_dColors.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/dabo/dColors.py
r2868 r2869 1 import re 1 import re, types 2 2 3 3 class HexError(Exception): pass … … 165 165 166 166 def hexToDec(hx): 167 if not isinstance(hx, types.StringTypes): 168 raise TypeError, "Input must be a string" 167 169 # Define a dict of char-value pairs 168 170 hex = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, … … 173 175 pos = 1 174 176 for c in rev: 177 if hex.get(c) == None: 178 raise InvalidCharError, "%s is an invalid hex character" % (c, ) 175 179 ret += (hex[c] * pos) 176 180 pos = pos * 16 … … 180 184 def tupleToHex(t, includeHash=True): 181 185 """Convert a color tuple into an HTML hex format.""" 186 if not len(t) == 3: 187 raise LengthError, "Color tuple needs to contain 3 elements" 188 for rgb in t: 189 if not isinstance(rgb, types.IntType): 190 raise IntegerTypeError, "Tuple elements should be all integers." 191 if not 0 <= rgb <= 255: 192 raise RgbValueError, "Rgb Value must be in the range 0-255" 182 193 rx, gx, bx = hex(t[0]), hex(t[1]), hex(t[2]) 183 194 # Each is in the format '0x00'. … … 185 196 g = gx[2:].upper() 186 197 b= bx[2:].upper() 198 if len(r) == 1: r = '0' + r 199 if len(g) == 1: g = '0' + g 200 if len(b) == 1: b = '0' + b 187 201 ret = "" 188 202 if includeHash: … … 195 209 # Strip the pound sign, if any 196 210 hx = hx.replace("#", "") 211 hx = hx.lstrip('0') 212 if len(hx) < 6: hx = '0'*(6-len(hx)) + hx 197 213 red = hexToDec(hx[:2]) 198 214 green = hexToDec(hx[2:4]) trunk/tests/unitTests/Test_dColors.py
r2868 r2869 14 14 import unittest 15 15 from dabo import dColors 16 import random 16 17 17 18 … … 87 88 knownValues = (("#000000",(0,0,0)), ("#000001",(0,0,1)), ("#000101",(0,1,1)), ("#010101",(1,1,1)), 88 89 ("#898989",(137,137,137)), ("#FFFFFF",(255,255,255)), ("#AAAAAA",(170,170,170)), 89 ("#A9A9A9",(169,169,169)), ("# A0A0A0",(10,10,10)), ("#090909",(9,9,9)),90 ("#A9A9A9",(169,169,169)), ("#0A0A0A",(10,10,10)), ("#090909",(9,9,9)), 90 91 ("#1A2BF3",(26,43,243)), ("#3F029E",(63,02,158)), ("#707070",(112,112,112)), 91 92 ("#6F6F6F",(111,111,111)), ("#8A8A8A",(138,138,138)), ("#00FF80",(0,255,128)), … … 100 101 101 102 def testKnownValuesHexToTuple(self): 102 """ tupleToHex should give known result with known input"""103 """colorTupleFromHex should give known result with known input""" 103 104 for hex, tuple in self.knownValues: 104 105 result = dColors.colorTupleFromHex(hex) … … 106 107 107 108 def testSanity(self): 108 """tuple = tupleToHex(colorTupleFromHex(tuple)) for allvalid input values"""109 for a in range(256):110 for b in range(256):111 for c in range(256):112 self.assertEqual((a,b,c), dColors.colorTupleFromHex(dColors.tupleToHex((a,b,c))))109 """tuple = tupleToHex(colorTupleFromHex(tuple)) for range of valid input values""" 110 #All inputs was way too many 111 for run in range(1000): 112 a, b, c = random.choice(range(256)), random.choice(range(256)), random.choice(range(256)) 113 self.assertEqual((a,b,c), dColors.colorTupleFromHex(dColors.tupleToHex((a,b,c)))) 113 114 114 115 #Test Errors … … 142 143 def testHexToTupleLeadingZeros(self): 143 144 """testing colorTupleFromHex(number) = colorTupleFromHex('00'+number)""" 144 number = " FF"145 number = "0A0CFF" 145 146 result = dColors.colorTupleFromHex(number) 146 147 zeroResult = dColors.colorTupleFromHex("000" + number)
