SHARE
TWEET

colorGrad function

a guest Jan 7th, 2017 134 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Take in a string to colorize and two hexidecimals in string format
  2. -- str     : string to colorize
  3. -- l_color : string, hex format "RRGGBB", color for leftmost char
  4. -- r_color : string, hex format "RRGGBB", color for rightmost char
  5. -- type    : int, gradient type, 0 is RGB (d), 1 is HSV, 2 is inverse HSV
  6. -- Return  : a string such that hecho will display the original string
  7. --    colored so that it fades from the leftmost character being
  8. --    l_color to the rightmost character being r_color, producing as
  9. --    smooth a gradient as possible, in the type requested.
  10.  
  11. function colorGrad(str, l_color, r_color, type)
  12.     type = type or 0
  13.     product = ""
  14.  
  15.     -- extract RGB values
  16.     l_r = tonumber(string.sub(l_color, 1, 2), 16)
  17.     l_g = tonumber(string.sub(l_color, 3, 4), 16)
  18.     l_b = tonumber(string.sub(l_color, 5, 6), 16)
  19.  
  20.     r_r = tonumber(string.sub(r_color, 1, 2), 16)
  21.     r_g = tonumber(string.sub(r_color, 3, 4), 16)
  22.     r_b = tonumber(string.sub(r_color, 5, 6), 16)
  23.  
  24.     if type == 0 then -- RGB Gradient
  25.  
  26.         -- The rate of change of color from each character to the next
  27.         m_r = (r_r - l_r) / (#str -1)
  28.         m_g = (r_g - l_g) / (#str -1)
  29.         m_b = (r_b - l_b) / (#str -1)
  30.  
  31.         -- The starting color info
  32.         b_r = l_r
  33.         b_g = l_g
  34.         b_b = l_b
  35.  
  36.         -- Perform the gradient per character
  37.         for i = 1, #str do
  38.             f_r = string.format("%x", m_r * (i-1) + b_r)
  39.             if #f_r < 2 then f_r = "0"..f_r end
  40.  
  41.             f_g = string.format("%x", m_g * (i-1) + b_g)
  42.             if #f_g < 2 then f_g = "0"..f_g end
  43.  
  44.             f_b = string.format("%x", m_b * (i-1) + b_b)
  45.             if #f_b < 2 then f_b = "0"..f_b end
  46.  
  47.             product = product.."|c"
  48.             product = product..f_r
  49.             product = product..f_g
  50.             product = product..f_b
  51.             product = product..string.sub(str, i, i)
  52.         end
  53.  
  54.     else -- HSV Gradients, both normal and Inverse
  55.         l_h, l_s, l_v = RGBtoHSV(l_r, l_g, l_b)
  56.         r_h, r_s, r_v = RGBtoHSV(r_r, r_g, r_b)
  57.  
  58.         -- Decide between standard (clockwise) or inverse (anti-clockwise)
  59.         if type == 1 then -- standard
  60.             -- We want to go clockwise along the color wheel, increasing l_h until we get to r_h
  61.             h_delta = r_h - l_h
  62.             if h_delta < 0 then
  63.                 h_delta = h_delta + 360
  64.             end
  65.             h_delta = h_delta / (#str - 1)
  66.  
  67.             s_delta = (r_s - l_s) / (#str - 1)
  68.             v_delta = (r_v - l_v) / (#str - 1)
  69.  
  70.             for i = 1, #str do
  71.                 n_r, n_g, n_b = HSVtoRGB(l_h + (h_delta * (i-1)), l_s + (s_delta * (i-1)), l_v + (v_delta * (i-1)))
  72.                 n_r = string.format("%x", n_r)
  73.                 n_g = string.format("%x", n_g)
  74.                 n_b = string.format("%x", n_b)
  75.                 if #n_r < 2 then n_r = "0"..n_r end
  76.                 if #n_g < 2 then n_g = "0"..n_g end
  77.                 if #n_b < 2 then n_b = "0"..n_b end
  78.  
  79.                 product = product.."|c"
  80.                 product = product..n_r
  81.                 product = product..n_g
  82.                 product = product..n_b
  83.                 product = product..string.sub(str, i, i)
  84.             end
  85.         elseif type == 2 then -- inverse
  86.             h_delta = l_h - r_h
  87.             if h_delta < 0 then
  88.                 h_delta = h_delta + 360
  89.             end
  90.             h_delta = h_delta / (#str - 1)
  91.  
  92.             s_delta = (l_s - r_s) / (#str - 1)
  93.             v_delta = (l_v - r_v) / (#str - 1)
  94.  
  95.             for i = 1, #str do
  96.                 n_r, n_g, n_b = HSVtoRGB(l_h - (h_delta * (i-1)), l_s - (s_delta * (i-1)), l_v - (v_delta * (i-1)))
  97.                 n_r = string.format("%x", n_r)
  98.                 n_g = string.format("%x", n_g)
  99.                 n_b = string.format("%x", n_b)
  100.                 if #n_r < 2 then n_r = "0"..n_r end
  101.                 if #n_g < 2 then n_g = "0"..n_g end
  102.                 if #n_b < 2 then n_b = "0"..n_b end
  103.  
  104.                 product = product.."|c"
  105.                 product = product..n_r
  106.                 product = product..n_g
  107.                 product = product..n_b
  108.                 product = product..string.sub(str, i, i)
  109.             end
  110.         end
  111.        
  112.     end
  113.     return product
  114. end
  115.  
  116.  
  117. function RGBtoHSV(r, g, b)
  118.     r = r / 255
  119.     g = g / 255
  120.     b = b / 255
  121.  
  122.     c_max = (r > g) and r or g
  123.  
  124.     c_max = (c_max > b) and c_max or b
  125.  
  126.     c_min = (r < g) and r or g
  127.     c_min = (c_min   < b) and c_min or b
  128.  
  129.     delta = c_max - c_min
  130.  
  131.  
  132.     -- Calculate hue
  133.     if delta == 0 then
  134.         h = 0
  135.     elseif r == c_max then
  136.         h = 60 * (((g - b) / delta) % 6)
  137.     elseif g == c_max then
  138.         h = 60 * (((b - r) / delta) + 2)
  139.     else
  140.         h = 60 * (((r - g) / delta) + 4)
  141.     end
  142.     if h < 0 then
  143.         h = h + 360
  144.     end
  145.  
  146.     -- Calculate Saturation
  147.     if c_max == 0 then
  148.         s = 0
  149.     else
  150.         s = delta / c_max
  151.     end
  152.  
  153.     v = c_max
  154.  
  155.     return h, s, v
  156.    
  157. end
  158.  
  159.  
  160. function HSVtoRGB(h, s, v)
  161.  
  162.     if h < 0 then
  163.         h = h + 360
  164.     elseif h > 360 then
  165.         h = h - 360
  166.     end
  167.  
  168.     if h == 360 then
  169.         h = 0
  170.     end
  171.  
  172.     C = v * s
  173.  
  174.     X = C * (1 - math.abs( (h/60) % 2 - 1 ))
  175.  
  176.     m = v - C
  177.    
  178.     if h < 60 then
  179.         r, g, b = C, X, 0
  180.     elseif h < 120 then
  181.         r, g, b = X, C, 0
  182.     elseif h < 180 then
  183.         r, g, b = 0, C, X
  184.     elseif h < 240 then
  185.         r, g, b = 0, X, C
  186.     elseif h < 300 then
  187.         r, g, b = X, 0, C
  188.     else
  189.         r, g, b = C, 0, X
  190.     end
  191.  
  192.     R = math.floor((r + m) * 255 + 0.5)
  193.     G = math.floor((g + m) * 255 + 0.5)
  194.     B = math.floor((b + m) * 255 + 0.5)
  195.  
  196.     return R, G, B
  197.  
  198. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top