Try, this script, then
tap on the button, then on the syringe
you will receive a viewfinder (no way to get it in the print screen ) that you move on one of your squares,
tap ok, the real rgb will appear, surprise, no one is #00FF00
# coding: utf-8
from objc_util import *
import ui
def colorPickerViewControllerDidFinish_(_self, _cmd, _controller):
#print('colorPickerViewControllerDidFinish')
UIColorPickerViewController = ObjCInstance(_controller)
def colorPickerViewControllerDidSelectColor_(_self, _cmd, _controller):
#print('colorPickerViewControllerDidSelectColor')
UIColorPickerViewController = ObjCInstance(_controller)
cl = UIColorPickerViewController.selectedColor()
rgb = (cl.red(),cl.green(),cl.blue())
coloredButtonItem(UIColorPickerViewController.buttonItem, rgb)
methods = [colorPickerViewControllerDidFinish_, colorPickerViewControllerDidSelectColor_]
protocols = ['UIColorPickerViewControllerDelegate']
try:
MyUIColorPickerViewControllerDelegate = ObjCClass('MyUIColorPickerViewControllerDelegate')
except Exception as e:
MyUIColorPickerViewControllerDelegate = create_objc_class('MyUIColorPickerViewControllerDelegate', methods=methods, protocols=protocols)
def coloredButtonItem(btn,rgb):
import ui
if rgb:
with ui.ImageContext(32,32) as ctx:
path = ui.Path.rect(0,0,32,32)
ui.set_color(rgb)
path.fill()
btn.image = ctx.get_image().with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
def UIColorPickerViewController(w, h, alpha_slider=True, title='', rgb=None, popover=None):
v = ui.View()
v.name = title
v.rgb = rgb
vc = ObjCInstance(v)
colorpicker = ObjCClass('UIColorPickerViewController').new().autorelease()
#print(dir(colorpicker))
delegate = MyUIColorPickerViewControllerDelegate.alloc().init()
colorpicker.delegate = delegate
colorpicker.setSupportsAlpha_(alpha_slider)
if rgb:
color = ObjCClass('UIColor').colorWithRed_green_blue_alpha_(rgb[0], rgb[1], rgb[2], 1.0)
colorpicker.setSelectedColor_(color)
clview = colorpicker.view()
v.frame = (0,0,w,h)
vc.addSubview_(clview)
done_button = ui.ButtonItem(title='ok')
def tapped(sender):
cl = colorpicker.selectedColor()
v.rgb = (cl.red(),cl.green(),cl.blue())
v.close()
done_button.action = tapped
color_button = ui.ButtonItem()
coloredButtonItem(color_button,rgb)
v.right_button_items = [done_button,color_button]
colorpicker.buttonItem = color_button
if popover:
x,y = popover
v.present('popover', popover_location=(x,y))
else:
v.present('sheet')
v.wait_modal()
return v.rgb
def rgb_to_hex(rgb):
r,g,b = rgb
r = int(255*r)
g = int(255*g)
b = int(255*b)
return f'#{r:02x}{g:02x}{b:02x}'
class Colors (ui.View):
def __init__(self, *args, **kwargs):
return super().__init__(self, *args, **kwargs)
def draw(self):
neon = ui.Path.rect(5, 5, 100, 100)
ui.set_color((0, 2, 0))
neon.fill()
normal = ui.Path.rect(110, 5, 100, 100)
ui.set_color(("#00ff00"))
normal.fill()
# Protect against import
if __name__ == '__main__':
example = Colors(bg_color="white")
lbl = ui.Label(frame=(5,120,200,32))
lbl.alignment = ui.ALIGN_CENTER
example.add_subview(lbl)
b1 = ui.ButtonItem()
b1.title = 'π'
def b1_action(sender):
# compute x,y of an ui.ButtonItem
vi = ObjCInstance(sender).view()
r = vi.convertRect_toView_(vi.bounds(),ObjCInstance(example))
# x,y in screen coordinates, not in view
x,y,w,h = r.origin.x,r.origin.y,r.size.width,r.size.height
x,y = x+w/2, y+h
w,h = 200,50
rgb = UIColorPickerViewController(w,h, alpha_slider=False, popover=(x,y))
if rgb:
lbl.text = rgb_to_hex(rgb)
b1.action = b1_action
example.right_button_items =(b1,)
example.present('fullscreen')
