Skip to content

Instantly share code, notes, and snippets.

@q66
Created May 11, 2017 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save q66/19203a08a3cf5a9092bccb800c143b6a to your computer and use it in GitHub Desktop.
Save q66/19203a08a3cf5a9092bccb800c143b6a to your computer and use it in GitHub Desktop.
-- generates a string to be used as weechat.color.chat_nick_colors value
-- when using 256 color terminals; includes all hues, skips #000000 (16)
--
-- arguments: <min_saturation>:<max_saturation> <min_value>:<max_value>
-- as in HSV, the 4 values are between 0 and 1
-- enable if you want grayscale (232-255) to be included
local include_gray = false
-- base weechat nick colors (maps to 0-15)
local base_colors = "cyan,magenta,green,brown,lightblue,default,"
.. "lightcyan,lightmagenta,lightgreen,blue"
local usage = function()
print(
"usage: "
.. (arg[0] or "nickcolors.lua")
.. " min_saturation:max_saturation min_value:max_value"
)
os.exit(1)
end
local get_sv = function(r, g, b)
local minv = math.min(r, math.min(g, b))
local maxv = math.max(r, math.max(g, b))
return {
(maxv - minv) / maxv, maxv / 0xFF
}
end
local generate_colors = function()
local r = {}
local steps = { 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF }
for i = 1, #steps do
for j = 1, #steps do
for k = 1, #steps do
r[#r + 1] = get_sv(steps[i], steps[j], steps[k])
end
end
end
if include_gray then
for i = 0x08, 0xEE, 0xA do
r[#r + 1] = { 0, i / 0xFF }
end
end
return r
end
local colors = generate_colors()
local sat, val = arg[1], arg[2]
if not sat or not val then
usage()
end
local min_sat, max_sat = sat:match("(.+):(.+)")
if not min_sat then
usage()
end
local min_val, max_val = val:match("(.+):(.+)")
if not min_val then
usage()
end
min_sat, max_sat = tonumber(min_sat), tonumber(max_sat)
min_val, max_val = tonumber(min_val), tonumber(max_val)
if not (min_sat and max_sat and min_val and max_val) then
usage()
end
local out_color = function(n)
os.execute("tput setab " .. n .. "; printf \"," .. n .. "\"")
end
os.execute("printf \"" .. base_colors .. "\"")
-- skip black (color 16), hence 2
for i = 2, #colors do
local s, v = unpack(colors[i])
if s >= min_sat and s <= max_sat and v >= min_val and v <= max_val then
out_color(i + 15)
end
end
os.execute("echo")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment