0

I want to make a colour brighter by adding an integer to each RGB value of the variable but im not sure how to do it.

black = (0,0,0)

for i in black:
   black[i] += 50

print(black)

The expected output is (50, 50, 50)

martineau
  • 119,623
  • 25
  • 170
  • 301
Alim '
  • 33
  • 3
  • Tuples are immutable (can't be changed), so you'll need to replace the value with a new one and assign it to the name. i.e. `black = tuple(v+50 for v in black)` — of course you might not want to name it `black` anymore. `;¬)` – martineau Mar 14 '20 at 18:28
  • What is the issue, exactly? Have you tried anything, done any research? Stack Overflow is not a free code writing service. See: [ask], [help/on-topic], https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. – AMC Mar 14 '20 at 19:39
  • Does this answer your question? [Python: changing value in a tuple](https://stackoverflow.com/questions/11458239/python-changing-value-in-a-tuple) – AMC Mar 14 '20 at 19:39

2 Answers2

2

Tuples are immutable, you cannot change their values after they are created. Try creating a new tuple instead:

black = (0,0,0)

newcolor = (black[0] + 50, black[1] + 50, black[2] + 50)

print(newcolor)

Or with list comprehension:

black = (0,0,0)

newcolor = tuple(component + 50 for component in black)

print(newcolor)

Remember you can only max each component to 255 - use Saturation arithmetic to overcome this

Omer Tuchfeld
  • 2,886
  • 1
  • 17
  • 24
0

Tuples are immutable

If your code needs to change value of RGB dynamically, then preferred datatype is List

So use list instead:

black = [0,0,0]

for i in range(len(black)):
   black[i] += 50

print(black)
op:[50, 50, 50]

Note:If you use your code by just change tuple to list, Then you will get [150, 0, 0] instead [50, 50, 50], Because value of i will be 0 all 3 times, and for next run you will get runTime error

Shivam Seth
  • 677
  • 1
  • 8
  • 21