python - adding value limitations on list comprehension -


i trying find clean way of grabbing difference between values of 2 int tuples. tuples have limitations though represent rgb colors. have far:

tupleone = (255, 0, 255)  # magenta tupletwo = (255, 0, 0)  # red tuple([tupleone[x] - tupletwo[x] x in range(3)])  # makes blue 

the problem have here bounds checking since each color value must 0-255. i'm going if larger number subtracted smaller, return 0 given index. have been using test single index:

value = -10 max(0, min(color, 255))  # value = 0 

is can cleanly list comprehensions? or there better approach?

sure, combine you've got there:

tuple(max(0, min(tupleone[x] - tupletwo[x], 255)) x in range(3)) 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -