0

I've managed to adjust color when cursor hovers over a tkinter canvas rounded rectangle button using this:

def shade_rgb(rgb):
    """ Adjust shade of rgb by 25% (value 64 out of 256)
        :param rgb: tuple of red, green, blue integers 0 to 255
        :returns:   tuple of red, green, blue integers 0 to 255
    """
    converted = []
    for i in rgb:
        if i < 128:
            i = i + 64
        else:
            i = i - 64
        converted.append(i)
    return tuple(converted)

I've seen code of list comprehension and tuple generators which I believe would shorten the code considerably. However I can "comprehend" how to make that work?


Reply to comment

"Where did you get stuck?"

My attempt at generator was something like this:

return tuple(i = i + 64 if i<128 else i = i - 64 for i in rgb)

Usually in Python I would use:

i = i + 64

or:

i += 64

But apparently within a generators you enter an alternate universe and the rules of physics change to:

i + 64
WinEunuuchs2Unix
  • 1,801
  • 1
  • 17
  • 34
  • 1
    Does this answer your question? [What does "list comprehension" mean? How does it work and how can I use it?](https://stackoverflow.com/questions/34835951/what-does-list-comprehension-mean-how-does-it-work-and-how-can-i-use-it) – mkrieger1 Jun 13 '21 at 23:35
  • 1
    The rules of physics don't change; it's merely where you are in the data transfer process. – Prune Jun 14 '21 at 00:46
  • @Prune Ha "Data Transfer Process" is an alternate universe to me just now. – WinEunuuchs2Unix Jun 14 '21 at 01:44
  • I'm not sure what Prune's trying to say, but the syntax problem there is that you can't put a statement ([assignment](https://docs.python.org/3/reference/simple_stmts.html#assignment-statements)) somewhere that an expression should go (in a [conditional expression](https://docs.python.org/3/reference/expressions.html#conditional-expressions)). That's different from C for example where assignment is an expression, from what I hear. – wjandrea Jun 14 '21 at 02:49

3 Answers3

2
return (i + 64 if i < 128
               else i - 64
         for i in rgb)

Where did you get stuck?

Prune
  • 76,765
  • 14
  • 60
  • 81
  • I got stuck in dBase III+, Clipper, COBOL, BASIC, RPG and Assembly Language 35 years ago :). I had a one liner at first like you posted but I was getting errors so I gave up and did it the long way above. Thanks. – WinEunuuchs2Unix Jun 13 '21 at 23:38
  • I think I figured out where I got stuck and updated my question to reply to your question. – WinEunuuchs2Unix Jun 14 '21 at 00:30
2

Try this out :

return tuple([i+64 if i < 128 else i - 64 for i in rgb])
Shubham Periwal
  • 2,198
  • 2
  • 8
  • 26
  • I had similar one-liner that had errors because I was using `i = i + 64` and `i = i * 64`. Interesting you get away with `i+64` instead of `i+=64` as happens with in-line code. – WinEunuuchs2Unix Jun 13 '21 at 23:41
  • 1
    You don't need the `[ ]` brackets. You will get the same tuple with `tuple(i+64 if i < 128 else i - 64 for i in rgb)` – Alexander S. Brunmayr Jun 13 '21 at 23:49
  • 2
    @AlexanderS.Brunmayr although, *the list comprehension is probably faster* – juanpa.arrivillaga Jun 14 '21 at 02:06
  • @juanpa.arrivillaga Thank you for your comment, I didn't know! I checked that making a tuple from a generator is around 25-35% slower than making a tuple from a list comprehension when rgb is a 3-tuple. So, everyone please include the [ ] brackets, as written in the answer by Shubham Periwal. – Alexander S. Brunmayr Jun 14 '21 at 07:29
2

You can pass a generator expression to the tuple constructor.

return tuple(i+64 if i<128 else i-64 for i in rgb)

There's no direct syntax for a tuple comprehension, probably because tuples usually contain heterogeneous data, while lists usually contain homogeneous data.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Bonsoir, Viva la Habs. Thanks for helping me out in **Ask Ubuntu** when I first started learning Linux five years ago. I'm accepting your answer, which is identical to the other two answers posted a few minute earlier, because you included references (cited sources). Merci. – WinEunuuchs2Unix Jun 13 '21 at 23:49
  • @Win De rien! (Mais, on dit «*vive les* Habs».) It's actually not quite identical, though the differences are subtle in this case. Prune wrote a straight-up generator expression, and Shubham wrote a list comprehension which he then converts to `tuple`. – wjandrea Jun 13 '21 at 23:53