Skip to content

Instantly share code, notes, and snippets.

@Phuket2
Created September 22, 2016 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phuket2/78009254f8bb3d5a76b075b75f59337b to your computer and use it in GitHub Desktop.
Save Phuket2/78009254f8bb3d5a76b075b75f59337b to your computer and use it in GitHub Desktop.
safe_css_color.py
'''
Pythonista Forum - @Phuket2
'''
import ui
_css_colors=['rosybrown', 'antiquewhite', 'lightsteelblue', 'white', 'darkblue', 'darkviolet', 'plum', 'darkcyan', 'blanchedalmond', 'chocolate', 'sienna', 'tomato', 'peachpuff', 'lightyellow', 'bisque', 'aqua', 'oldlace', 'maroon', 'palegreen', 'chartreuse', 'darkturquoise', 'linen', 'magenta', 'lemonchiffon', 'powderblue', 'papayawhip', 'gold', 'khaki', 'lightseagreen', 'darkred', 'floralwhite', 'turquoise', 'mediumspringgreen', 'indianred', 'lightgreen', 'crimson', 'mintcream', 'lavender', 'purple', 'orchid', 'darkslateblue', 'whitesmoke', 'moccasin', 'beige', 'mistyrose', 'dodgerblue', 'hotpink', 'lightcoral', 'goldenrod', 'coral', 'cadetblue', 'black', 'mediumseagreen', 'gainsboro', 'paleturquoise', 'darkgreen', 'darkkhaki', 'mediumblue', 'dimgray', 'darkorchid', 'deeppink', 'mediumvioletred', 'lightgray', 'darkgrey', 'lightsalmon', 'lightblue', 'lightslategrey', 'slategray', 'slateblue', 'greenyellow', 'darkgray', 'lawngreen', 'cornflowerblue', 'midnightblue', 'lightpink', 'deepskyblue', 'navy', 'lightskyblue', 'darkorange', 'blueviolet', 'lightgrey', 'lightgoldenrodyellow', 'violet', 'ivory', 'mediumslateblue', 'cyan', 'rebeccapurple', 'firebrick', 'green', 'burlywood', 'wheat', 'mediumpurple', 'mediumturquoise', 'skyblue', 'peru', 'forestgreen', 'royalblue', 'aquamarine', 'silver', 'olive', 'palevioletred', 'mediumorchid', 'darkslategray', 'darkslategrey', 'steelblue', 'olivedrab', 'lime', 'orangered', 'grey', 'sandybrown', 'slategrey', 'pink', 'blue', 'palegoldenrod', 'ghostwhite', 'brown', 'darkseagreen', 'saddlebrown', 'salmon', 'cornsilk', 'red', 'snow', 'tan', 'aliceblue', 'yellow', 'yellowgreen', 'springgreen', 'thistle', 'navajowhite', 'teal', 'lightcyan', 'orange', 'darksalmon', 'mediumaquamarine', 'darkolivegreen', 'lavenderblush', 'indigo', 'fuchsia', 'honeydew', 'azure', 'lightslategray', 'seagreen', 'gray', 'dimgrey', 'limegreen', 'darkmagenta', 'darkgoldenrod', 'seashell']
def safe_color(css_color):
rgba = ui.parse_color(css_color)
r = rgba[0]
g = rgba[1]
b = rgba[2]
if (((r + g + b) / 3 ) > .55) :
return 'black'
else:
return 'white'
class ScrollViewClass(ui.View):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class MyClass(ui.View):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.clr_list = sorted(_css_colors)
def draw(self):
sq =60
r = ui.Rect(0, 0, 32, 32)
cnt = 0
for y in range(0, 15):
for x in range(0, 10):
r.x = r.width * x
r.y = r.height * y
self.draw_item(r, self.clr_list[cnt], 'A')
cnt += 1
if cnt is len(self.clr_list):
break
def draw_item(self, r, color, text):
# hmmm, i think ui.draw_string is doing something a little
# stange with the underlying color, not sure
s = ui.Path.rect(*r)
ui.set_color(color)
s.fill()
fnt = ('Futura', 24)
fnt_color = safe_color(color)
ui.draw_string('A', rect=r, font=fnt, color=fnt_color, alignment=ui.ALIGN_CENTER, line_break_mode=ui.LB_TRUNCATE_TAIL)
if __name__ == '__main__':
w, h = 320, 480
f = (0, 0, w, h)
mc = MyClass(frame=f, bg_color='white', name = 'CSS Safe Color Test')
mc.present('sheet', animated=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment