| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | DEFAULT_COLOR = '#3c581a' |
|---|
| 19 | |
|---|
| 20 | def clamp(minValue, maxValue, value): |
|---|
| 21 | """Make sure value is between minValue and maxValue""" |
|---|
| 22 | if value < minValue: |
|---|
| 23 | return minValue |
|---|
| 24 | if value > maxValue: |
|---|
| 25 | return maxValue |
|---|
| 26 | return value |
|---|
| 27 | |
|---|
| 28 | def hex2rgb(hexstring, digits=2): |
|---|
| 29 | """Converts a hexstring color to a rgb tuple. |
|---|
| 30 | |
|---|
| 31 | Example: #ff0000 -> (1.0, 0.0, 0.0) |
|---|
| 32 | |
|---|
| 33 | digits is an integer number telling how many characters should be |
|---|
| 34 | interpreted for each component in the hexstring. |
|---|
| 35 | """ |
|---|
| 36 | if isinstance(hexstring, (tuple, list)): |
|---|
| 37 | return hexstring |
|---|
| 38 | |
|---|
| 39 | top = float(int(digits * 'f', 16)) |
|---|
| 40 | r = int(hexstring[1:digits+1], 16) |
|---|
| 41 | g = int(hexstring[digits+1:digits*2+1], 16) |
|---|
| 42 | b = int(hexstring[digits*2+1:digits*3+1], 16) |
|---|
| 43 | return r / top, g / top, b / top |
|---|
| 44 | |
|---|
| 45 | def lighten(r, g, b, amount): |
|---|
| 46 | """Return a lighter version of the color (r, g, b)""" |
|---|
| 47 | return (clamp(0.0, 1.0, r + amount), |
|---|
| 48 | clamp(0.0, 1.0, g + amount), |
|---|
| 49 | clamp(0.0, 1.0, b + amount)) |
|---|
| 50 | |
|---|
| 51 | def generateColorscheme(masterColor, keys): |
|---|
| 52 | """Generates a dictionary where the keys match the keys argument and |
|---|
| 53 | the values are colors derivated from the masterColor. |
|---|
| 54 | |
|---|
| 55 | Each color is a lighter version of masterColor. This difference is |
|---|
| 56 | computed based on the number of keys. |
|---|
| 57 | |
|---|
| 58 | The masterColor is given in a hex string format. |
|---|
| 59 | """ |
|---|
| 60 | r, g, b = hex2rgb(masterColor) |
|---|
| 61 | light = 1.0 / (len(keys)*2) |
|---|
| 62 | return dict([(key, lighten(r, g, b, light * i)) |
|---|
| 63 | for i, key in enumerate(keys)]) |
|---|
| 64 | |
|---|
| 65 | def defaultColorscheme(keys): |
|---|
| 66 | """Return the default color scheme (derived from a dark green)""" |
|---|
| 67 | return generateColorscheme(DEFAULT_COLOR, keys) |
|---|
| 68 | |
|---|
| 69 | def getColorscheme(color, keys): |
|---|
| 70 | """Get a color scheme from the six predefined ones or makes another |
|---|
| 71 | one if the color is not found |
|---|
| 72 | """ |
|---|
| 73 | return generateColorscheme(colorSchemes.get(color, color), keys) |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | colorSchemes = dict( |
|---|
| 77 | red='#6d1d1d', |
|---|
| 78 | green=DEFAULT_COLOR, |
|---|
| 79 | blue='#224565', |
|---|
| 80 | grey='#444444', |
|---|
| 81 | black='#000000', |
|---|
| 82 | darkcyan='#305755' |
|---|
| 83 | ) |
|---|