I wrote a script to iterate through the 3 RGB values with pre-set increments.
You'll notice a copy/paste of six loops with a change in each one.
I imagine these six sections could be combined into one or two parent loops to increment through one child loop instead of 6. Does that make sense?
My goal is to reduce the size of this code.
Here is the code:
color_numbers = [0,255]
increment_between_values = [64,64,63,64]
r_val=[]
g_val=[]
b_val=[]
rgb_values=[]#FORMAT: 255,255,255
def color_function():
color_base_b = color_numbers[0]
color_top_g = color_numbers[1]
color_base_r = color_numbers[0]
for x in range(len(increment_between_values)):
r_val.append(color_numbers[0])
g_val.append(color_numbers[1])
color_base_b += increment_between_values[x]
b_val.append(color_base_b)
for x in range(len(increment_between_values)):
r_val.append(color_numbers[0])
color_top_g -= increment_between_values[x]
g_val.append(color_top_g)
b_val.append(color_base_b)
for x in range(len(increment_between_values)):
color_base_r += increment_between_values[x]
r_val.append(color_base_r)
g_val.append(color_top_g)
b_val.append(color_base_b)
for x in range(len(increment_between_values)):
r_val.append(color_base_r)
g_val.append(color_top_g)
color_base_b -= increment_between_values[x]
b_val.append(color_base_b)
for x in range(len(increment_between_values)):
r_val.append(color_base_r)
color_top_g += increment_between_values[x]
g_val.append(color_top_g)
b_val.append(color_base_b)
for x in range(len(increment_between_values)):
color_base_r -= increment_between_values[x]
r_val.append(color_base_r)
g_val.append(color_top_g)
b_val.append(color_base_b)
for t in range(len(r_val)):
print(str(r_val[t])+","+str(g_val[t])+","+str(b_val[t]))
color_function()
From what I see, the change in each loop goes as follows: loop 1: initial value loop 2: initial value loop 3: increment up loop 4: top value loop 5: top value loop 6: increment down
These six steps are offset for each of the R,G and B For example: When R is on loop 1, G is on loop 3, and B is on loop 5. There is a second loop that switches from + to - (increment up, increment down)
I hope I've explained this clearly The code works as is, I'd just like to understand how it could be optimized.
Your variable names are distractingly long, misleading, and used inconsistently. And the looping by index is bad. A suggestion:
def color_function():
r = b = color_numbers[0]
g = color_numbers[1]
def append():
r_val.append(r)
g_val.append(g)
b_val.append(b)
for inc in increment_between_values:
b += inc
append()
# the other five loops similarly...
for rgb in zip(r_val, g_val, b_val):
print(*rgb, sep=",")
Still six loops, but much shorter/clearer, easy to see what's happening.