code from ariel
Preview
Download here: gradient.py
Back to the article: 88x31 button you can draw on
import requests
import pandas
import math
Y_MAX = 30
X_MAX = 87
def collect_hex():
colors_hex = [input("Enter the hex code for the first color: ").strip("# "), input("Enter the hex code for the second color: ").strip("# "), input("Enter the hex code for the third color: ").strip("# "), input("Enter the hex code for the fourth color: ").strip("# "), input("Enter the hex code for the fifth color: ").strip("# ")]
for color in colors_hex:
if color:
if len(color)!=6:
print("😡")
return False
for char in color:
if char not in "1234567890abcdefABCDEF":
print("😡")
return False
return colors_hex
def convert_to_rgb(colors_hex):
colors_rgb = []
for color in colors_hex:
if color:
rgb = tuple(int(color[i:i+2], 16) for i in (0, 2, 4))
colors_rgb.append(rgb)
else:
colors_rgb.append(None)
return colors_rgb
def initiate_pixels(colors):
df = pandas.DataFrame(columns=['x','R','G','B'])
coords = {0:0,1:22,2:44,3:66,4:87}
for index, x in coords.items():
if colors[index]:
R,G,B = colors[index]
new_row = {'x':x,'R':R,'G':G,'B':B}
df = df._append(new_row, ignore_index=True)
print(df)
return df
def fill(df):
if df.shape[0]==5:
for i in range(5):
new_rows=[]
for index,_ in df.iterrows():
try:
x=math.floor((df.iloc[index,0]+df.iloc[index+1,0])/2)
R=math.floor((df.iloc[index,1]+df.iloc[index+1,1])/2)
G=math.floor((df.iloc[index,2]+df.iloc[index+1,2])/2)
B=math.floor((df.iloc[index,3]+df.iloc[index+1,3])/2)
new_row = {'x':x,'R':R,'G':G,'B':B}
new_rows.append(new_row)
except:
pass
for new_row in new_rows:
df = df._append(new_row, ignore_index=True)
df = df.sort_values('x')
elif df.shape[0]==2:
for i in range(7):
new_rows=[]
for index,_ in df.iterrows():
try:
x=math.floor((df.iloc[index,0]+df.iloc[index+1,0])/2)
R=math.floor((df.iloc[index,1]+df.iloc[index+1,1])/2)
G=math.floor((df.iloc[index,2]+df.iloc[index+1,2])/2)
B=math.floor((df.iloc[index,3]+df.iloc[index+1,3])/2)
new_row = {'x':x,'R':R,'G':G,'B':B}
new_rows.append(new_row)
except:
pass
for new_row in new_rows:
df = df._append(new_row, ignore_index=True)
df = df.sort_values('x')
else:
for i in range(6):
new_rows=[]
for index,_ in df.iterrows():
try:
x=math.floor((df.iloc[index,0]+df.iloc[index+1,0])/2)
R=math.floor((df.iloc[index,1]+df.iloc[index+1,1])/2)
G=math.floor((df.iloc[index,2]+df.iloc[index+1,2])/2)
B=math.floor((df.iloc[index,3]+df.iloc[index+1,3])/2)
new_row = {'x':x,'R':R,'G':G,'B':B}
new_rows.append(new_row)
except:
pass
for new_row in new_rows:
df = df._append(new_row, ignore_index=True)
df = df.sort_values('x')
gradient = df.drop_duplicates('x')
#gradient.to_csv("gradient.csv")
print(gradient)
return gradient
def paint(gradient):
for y in range(Y_MAX+1):
for row in gradient.itertuples():
x,r,g,b = row.x,row.R,row.G,row.B
URL = f"https://cqql.site/chaosbtn/draw?x={x}&y={y}&r={r}&g={g}&b={b}"
print(URL)
response = requests.get(URL)
print(response)
def main():
hex = False
while not hex:
hex = collect_hex()
rgb = convert_to_rgb(hex)
print(rgb)
df = initiate_pixels(rgb)
gradient = fill(df)
paint(gradient)
if __name__=="__main__":
print("\n✨ Hi ✨\nHere's a scrpit for creating color gradients on cqql's chaos button:\n➡️ https://cqql.site/articles/chaosbutton_88x31.html\n\nYou can enter up to 5 hex codes (with or without #) to create a gradient based on them, going from left to right.\n\nPlease note that I'm a lazy kitten: when you enter an incorrect input, the script lets you continue and only gets angry at you later on. It's a very polite script though, so it lets you try again and again when you mess the hex code up and make it angry.\n\nYou can skip some of the colors by typing an empty string, but you need at least two colors (the first one and the fifth one) to make it work! 🌈\n")
main()