igogiko

gradient_post

Mar 7th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. import operator, struct
  2. from Tkinter import Tk
  3.  
  4. def RGBToHex(rgb):
  5.   return "%02x%02x%02x" % (rgb[0], rgb[1], rgb[2])
  6.  
  7. def hexToRGB(hex):
  8.   if hex[0] == "#": hex = hex[1:]
  9.   return struct.unpack("BBB",hex.decode("hex"))
  10.  
  11. def copyToClipboard(text):
  12.   r = Tk()
  13.   r.withdraw()
  14.   r.clipboard_clear()
  15.   r.clipboard_append(text)
  16.   r.destroy()
  17.  
  18. # The string to colorize
  19. string = "Write your text here!"
  20.  
  21. # Get string length
  22. length = len(string)
  23.  
  24. # Starting and ending color of the gradient, you can use hexToRGB() Here
  25. rgb1 = (0, 0, 0)
  26. rgb2 = (255, 255, 255)
  27.  
  28. # Calculate the differences of two colors
  29. diff = tuple(map(operator.sub, rgb1, rgb2))
  30. diff = tuple(map(operator.neg, diff))
  31. diff = [float (i) for i in diff]
  32.  
  33. # Get how much is each "step" for each color
  34. portions = (diff[0] / length, diff[1] / length, diff[2] / length)
  35.  
  36. # Main flow
  37. text = ""
  38. for i, char in enumerate(string):
  39.   if char == " ": # No operations needed
  40.     text += char
  41.     continue
  42.  
  43.   temp = (portions[0] * i, portions[1] * i, portions[2] * i)
  44.   temp = tuple(map(operator.add, rgb1, temp))
  45.   temp = [int (i) for i in temp]
  46.  
  47.   color = RGBToHex((temp[0], temp[1], temp[2]))
  48.  
  49.   text += "[color=#%s]%s[/color]" % (color, char)
  50.  
  51. # Self explanatory
  52. copyToClipboard(text)
Advertisement
Add Comment
Please, Sign In to add comment