Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Take in a string to colorize and two hexidecimals in string format
- -- str : string to colorize
- -- l_color : string, hex format "RRGGBB", color for leftmost char
- -- r_color : string, hex format "RRGGBB", color for rightmost char
- -- type : int, gradient type, 0 is RGB (d), 1 is HSV, 2 is inverse HSV
- -- Return : a string such that hecho will display the original string
- -- colored so that it fades from the leftmost character being
- -- l_color to the rightmost character being r_color, producing as
- -- smooth a gradient as possible, in the type requested.
- function colorGrad(str, l_color, r_color, type)
- type = type or 0
- product = ""
- -- extract RGB values
- l_r = tonumber(string.sub(l_color, 1, 2), 16)
- l_g = tonumber(string.sub(l_color, 3, 4), 16)
- l_b = tonumber(string.sub(l_color, 5, 6), 16)
- r_r = tonumber(string.sub(r_color, 1, 2), 16)
- r_g = tonumber(string.sub(r_color, 3, 4), 16)
- r_b = tonumber(string.sub(r_color, 5, 6), 16)
- if type == 0 then -- RGB Gradient
- -- The rate of change of color from each character to the next
- m_r = (r_r - l_r) / (#str -1)
- m_g = (r_g - l_g) / (#str -1)
- m_b = (r_b - l_b) / (#str -1)
- -- The starting color info
- b_r = l_r
- b_g = l_g
- b_b = l_b
- -- Perform the gradient per character
- for i = 1, #str do
- f_r = string.format("%x", m_r * (i-1) + b_r)
- if #f_r < 2 then f_r = "0"..f_r end
- f_g = string.format("%x", m_g * (i-1) + b_g)
- if #f_g < 2 then f_g = "0"..f_g end
- f_b = string.format("%x", m_b * (i-1) + b_b)
- if #f_b < 2 then f_b = "0"..f_b end
- product = product.."|c"
- product = product..f_r
- product = product..f_g
- product = product..f_b
- product = product..string.sub(str, i, i)
- end
- else -- HSV Gradients, both normal and Inverse
- l_h, l_s, l_v = RGBtoHSV(l_r, l_g, l_b)
- r_h, r_s, r_v = RGBtoHSV(r_r, r_g, r_b)
- -- Decide between standard (clockwise) or inverse (anti-clockwise)
- if type == 1 then -- standard
- -- We want to go clockwise along the color wheel, increasing l_h until we get to r_h
- h_delta = r_h - l_h
- if h_delta < 0 then
- h_delta = h_delta + 360
- end
- h_delta = h_delta / (#str - 1)
- s_delta = (r_s - l_s) / (#str - 1)
- v_delta = (r_v - l_v) / (#str - 1)
- for i = 1, #str do
- 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)))
- n_r = string.format("%x", n_r)
- n_g = string.format("%x", n_g)
- n_b = string.format("%x", n_b)
- if #n_r < 2 then n_r = "0"..n_r end
- if #n_g < 2 then n_g = "0"..n_g end
- if #n_b < 2 then n_b = "0"..n_b end
- product = product.."|c"
- product = product..n_r
- product = product..n_g
- product = product..n_b
- product = product..string.sub(str, i, i)
- end
- elseif type == 2 then -- inverse
- h_delta = l_h - r_h
- if h_delta < 0 then
- h_delta = h_delta + 360
- end
- h_delta = h_delta / (#str - 1)
- s_delta = (l_s - r_s) / (#str - 1)
- v_delta = (l_v - r_v) / (#str - 1)
- for i = 1, #str do
- 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)))
- n_r = string.format("%x", n_r)
- n_g = string.format("%x", n_g)
- n_b = string.format("%x", n_b)
- if #n_r < 2 then n_r = "0"..n_r end
- if #n_g < 2 then n_g = "0"..n_g end
- if #n_b < 2 then n_b = "0"..n_b end
- product = product.."|c"
- product = product..n_r
- product = product..n_g
- product = product..n_b
- product = product..string.sub(str, i, i)
- end
- end
- end
- return product.."|r"
- end
- function RGBtoHSV(r, g, b)
- r = r / 255
- g = g / 255
- b = b / 255
- c_max = (r > g) and r or g
- c_max = (c_max > b) and c_max or b
- c_min = (r < g) and r or g
- c_min = (c_min < b) and c_min or b
- delta = c_max - c_min
- -- Calculate hue
- if delta == 0 then
- h = 0
- elseif r == c_max then
- h = 60 * (((g - b) / delta) % 6)
- elseif g == c_max then
- h = 60 * (((b - r) / delta) + 2)
- else
- h = 60 * (((r - g) / delta) + 4)
- end
- if h < 0 then
- h = h + 360
- end
- -- Calculate Saturation
- if c_max == 0 then
- s = 0
- else
- s = delta / c_max
- end
- v = c_max
- return h, s, v
- end
- function HSVtoRGB(h, s, v)
- if h < 0 then
- h = h + 360
- elseif h > 360 then
- h = h - 360
- end
- if h == 360 then
- h = 0
- end
- C = v * s
- X = C * (1 - math.abs( (h/60) % 2 - 1 ))
- m = v - C
- if h < 60 then
- r, g, b = C, X, 0
- elseif h < 120 then
- r, g, b = X, C, 0
- elseif h < 180 then
- r, g, b = 0, C, X
- elseif h < 240 then
- r, g, b = 0, X, C
- elseif h < 300 then
- r, g, b = X, 0, C
- else
- r, g, b = C, 0, X
- end
- R = math.floor((r + m) * 255 + 0.5)
- G = math.floor((g + m) * 255 + 0.5)
- B = math.floor((b + m) * 255 + 0.5)
- return R, G, B
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement