3

I would like to draw a scale which ranges from red to green. I managed to do a scale which ranges from green to yellow. Now I have 2 possible solutions :

  • Either drawing 2 gradients : one from red to yellow and one from yellow to green. Then I can link the 2 drawings.

  • Or the better solution I think : Drawing one gradient from red to green with a checkpoint on the yellow.

How to implement this second solution ?

Here is my code from green to yellow. You can test it.

import tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        f = GradientFrame(root)
        f.pack(fill="both", expand=True)

class GradientFrame(tk.Canvas):
'''A gradient frame which uses a canvas to draw the background'''
    def __init__(self, parent, borderwidth=1, relief="sunken"):
    tk.Canvas.__init__(self, parent, borderwidth=borderwidth, relief=relief)
        self._color1 = "green"
        self._color2 = "yellow"
        self._color3 = "red"
        self.bind("<Configure>", self._draw_gradient)

def _draw_gradient(self, event=None):
    '''Draw the gradient'''
    self.delete("gradient")
    width = 200
    height = 50
    limit = width
    (r1,g1,b1) = self.winfo_rgb(self._color1)
    (r2,g2,b2) = self.winfo_rgb(self._color2)
    (r3,g3,b3) = self.winfo_rgb(self._color3)
    r_ratio = float((r2-r1)) / limit
    g_ratio = float((g2-g1)) / limit
    b_ratio = float((b2-b1)) / limit

    for i in range(limit):
        nr = int(r1 + (r_ratio * i))
        ng = int(g1 + (g_ratio * i))
        nb = int(b1 + (b_ratio * i))
        color = "#%4.4x%4.4x%4.4x" % (nr,ng,nb)
        self.create_line(0,i,height,i, tags=("gradient",), fill=color)
    self.lower("gradient")

    for i in range(limit):
        nr = int(r1 + (r_ratio * i))
        ng = int(g1 + (g_ratio * i))
        nb = int(b1 + (b_ratio * i))
        color = "#%4.4x%4.4x%4.4x" % (nr,ng,nb)
        self.create_line(0,i,height,i, tags=("gradient",), fill=color)
    self.lower("gradient")

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()
  • possible duplicate of [Generate colors between red and green for a power meter?](http://stackoverflow.com/questions/340209/generate-colors-between-red-and-green-for-a-power-meter) – jonrsharpe Feb 10 '15 at 16:42

1 Answers1

2

Without the generalization or general aesthetics of the above code, I offer you a simple solution (implemented against the red to green gradient) here:

import tkinter

def rgb(r, g, b):
    return "#%s%s%s" % tuple([hex(c)[2:].rjust(2, "0") for c in (r, g, b)])

root = tkinter.Tk()
root.title("Gradient")

gradient1 = tkinter.Canvas(root, width=255*2, height=50)
gradient1.pack()

gradient2 = tkinter.Canvas(root, width=255*2, height=50)
gradient2.pack()

for x in range(0, 256):
    r = x
    g = 255-x
    gradient1.create_rectangle(x*2, 0, x*2 + 2, 50, fill=rgb(r, g, 0), 
        outline=rgb(r, g, 0))

for x in range(0, 256):
    r = x*2 if x < 128 else 255
    g = 255 if x < 128 else 255 - (x-128)*2
    gradient2.create_rectangle(x*2, 0, x*2 + 2, 50, fill=rgb(r, g, 0), 
        outline=rgb(r, g, 0))

I believe it's what you meant by "yellow checkpoints". Thanks for the challenge! :^)

noahbkim
  • 528
  • 2
  • 13