local blue = math.min((b_step * step) + rgb1[3], 255) gradient[step + 1] = {red, green, blue} end end local colored_text = "" for pos, color in ipairs(gradient) do colored_text = colored_text .. "<" .. color[1] .. "," .. color[2] .. "," .. color[3] .. ">" .. string.sub(text, pos, pos) end return colored_text endfunction gradient(rgb1, rgb2, text) local clamp = function (num) return math.max(math.min(num, 255), 0) end local rgb1 = {clamp(rgb1[1]), clamp(rgb1[2]), clamp(rgb1[3])} local rgb2 = {clamp(rgb2[1]), clamp(rgb2[2]), clamp(rgb2[3])} local gradient = {rgb1} if #text == 1 then local rgb = {} for color = 1, 3 do rgb[color] = (rgb1[color] + rgb2[color]) / 2 end return "<" .. rgb[1] .. "," .. rgb[2] .. "," .. rgb[3] .. ">" .. text end if #text == 2 then gradient[2] = rgb2 else local length = #text - 1 local r_step = math.floor(((rgb2[1] - rgb1[1]) / length) + 0.5) local g_step = math.floor(((rgb2[2] - rgb1[2]) / length) + 0.5) local b_step = math.floor(((rgb2[3] - rgb1[3]) / length) + 0.5) for step = 1, length do local red = math.min((r_step * step) + rgb1[1], 255) local green = math.min((g_step * step) + rgb1[2], 255)
This expects to get two tables of numbers between 0-255 in {red, green, blue} format and the text you want formatted. It will return a string formatted for use with decho() in Mudlet. If you want it to just spit out the echo itself, change "return colored_text" to "decho(colored_text)". I hadn't seen this around and needed it for my system, so I threw it together at lunch, figured someone might find it neat.
Comments
Do you mind if I port and share your function for Nexus?
Added a loop function so the gradient turns around.
A bit verbose but works.
- Split the code into cgradient() and dgradient(), to work with cecho() or decho().
- Added the ability to generate the gradients as tables with cgradient_table() and dgradient_table(). This will output a table with integer keys corresponding to character number in the given string and values each being a table with the first index a three value table containing the RGB values and the second index the character itself, e.g. {{255, 255, 255}, "a"}.
- cgradient() and dgradient() will each accept a string argument followed by any number of color nodes formatted as tables of the form {r, g, b}.
Future plans:This is a comparison between dgradient() and cgradient(), respectively, over three different gradients. The middle is simple {255,0,0}-{0,0,255} red to blue, but the first shows off the ability to feed in multiple nodes, being an input of seven nodes: {255,0,0}, {255,128,0}, {255,255,0}, {0,255,0}, {0,255,255}, {0,128,255}, {128,0,255}. The final pair is simply {50,50,50}, {0,255,0}, {50,50,50}.
The same code is attached as a text file if that's more convenient.
decho(dgradient("sentence here", {50,50,50}, {0,255,0}, {50,50,50}))
Estarra the Eternal says, "Give Shevat the floor please."