Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Setting attributes of ui elements with RGB truples
-
Sorry I got it. Float division eg, 240.0 / 255.0 is very close. I just didn't know what to search for. Thanks for the help
-
The code at http://omz-forums.appspot.com/pythonista/post/5789626920861696 might help. It creates 752 Pythonista colors from tkinter. Part of its output is
light coral = (0.9411764705882353, 0.5019607843137255, 0.5019607843137255)
;-) -
To convert the tuple to the Pythonista format, you'd simply have to create a new tuple with each number divided by 255.
colour = (240,128,128) # as you would have had before colour = (colour[0]/255.0, colour[1]/255.0, colour[2]/255.0) # the actual conversion
EDIT: Here's a better version:
colour = (240,128,128) colour = tuple(entry/255.0 for entry in colour)
Using generator expressions not only makes it shorter, but also easier to read.
-
Thanks for all the feedback. Very useful. Been playing with
def calc_color(RGB, shade_percent): return tuple((entry / 255.0)* shade_percent for entry in RGB)
Seen the shading idea on stackflow. Looks like it could give some pleasant results with little effort. Don't really play with colours, so all new to me, but is fun
-
Ok, I am sure there is a lot to pick apart about my code. But just been experimenting and learning from the input I received here. I just did this small example. An achievement for me ...
import ui, clipboard, console from random import randint def calc_color(RGB, shade_percent): return tuple((entry / 255.0) * shade_percent for entry in RGB) _base_RGB_color = (128,255,128) _percent_increment = .02 ''' i had to declare and use _my_view, because i did not know how to recover the ui.View object from a ui.ButtonItem. i am guessing you cant, given it does not inherit from ui.View. i am sure a better way to do it , as most of my code ''' _my_view = None def std_btn(title = '', x=0,y=0): btn = ui.Button(title = title) btn.border_color = 'black' btn.border_width = .5 btn.width = 54 btn.height = 54 btn.font = ('<System-Bold>', 18) btn.x, btn.y = x,y btn.tint_color = 'blue' btn.action = btn_action return btn def random_rgb(): color = (0,0,0) return tuple(( entry + randint(0,255)) for entry in color) def btn_action(sender): clipboard.set(str(sender.background_color)) console.hud_alert('Copied', duration = 1) def redraw_grid(sender): v = _my_view color = random_rgb() v.name = str(color) percent = 0. for btn in v.subviews: if type(btn)== ui.Button: btn.title = str(percent) btn.background_color = calc_color(color, percent ) percent += _percent_increment draw_linear(_my_view, color) def draw_linear(v, color): percent = 0. for lb in v.subviews: if type(lb) == ui.Label: lb.background_color = calc_color(color, percent) percent += _percent_increment if __name__ == '__main__': color = random_rgb() v = ui.View(name=str(color)) btn = ui.ButtonItem('Random RGB Color') btn.action = redraw_grid v.right_button_items = [btn] # draw button grid percent = 0. for i in range(0,10): for j in range(0,10): ctl = std_btn(str(percent), x = j * 54, y = i * 54) ctl.tint_color = 'white' percent += _percent_increment v.add_subview(ctl) # draw linear stripe percent = 0. for i in range(0,100): lb = ui.Label(str(percent)) lb.width = 5.4 lb.height = 36 lb.y = 540 lb.x = i * 5.4 lb.border_width = 0 v.add_subview(lb) _my_view = v redraw_grid(v) v.present('sheet')
-
@Moe. Really,I love the syntax for the generator. I sort of see how great it is. But because I am so new, it will take a while to stick in my brain and more importantly for me to be able to use it more generically and naturally.I struggled to write the random_rgb() func using the generator. But, I think what I did in the end was correct. Having to incorporate a function threw me. If I over complicated it, I would love to hear back. Thanks again, great learning experience for me as an old guy :)
-
Simplifying
random_rgb()
:def random_rgb(): return randint(0, 255), randint(0, 255), randint(0, 255)
Also in the Pythonista beta, you need to add the line:
v.width = v.height = 540
near the bottom of the script or the view is too small to see or dismiss.
-
@ccc, thanks for the comments. Yeah, I can see your random_rgb() a lot simpler. I as just trying to get into the groove of using the other syntax. I can imagine for larger truples could be very nice. Just hard for me to think this way naturally at the moment. Need a lot more experience. But I will keep writing small programs like this to learn.
I had previously specified the width and height of the view before, but somehow i removed from my code when I was trying to clean up. I realise far to many literals in my code anyway. I really haven't got my head around the presentation styles and orientation switching as yet, let alone iPad vrs iphone.
But thanks again. -
If you like tuples, check out namedtuples...
import collections rgb = collections.namedtuple('rgb', 'red green blue') color = rgb(.1, .2, .3) print(color) # color.blue = .5 # can not modify a tuple
-
@ccc, thanks, I did look at namedtuple's before. I see many people have ideas about their performance. For me, is not an issue, as I am not doing anything demanding on the processor. But again, people say Python is easy to learn. And yes, you can do some amazing things with little knowledge, but in reality you need to learn a lot. Not to put anyone off, but still it is not as simple as people make out. I can sort of speak Thai (I am Australian) , but sort of... So many versions of being able to speak/understand/write a spoken language. I see correlations with Python language. That being said, I wish I had a language like this in the dark ages :) is inspired!