Software /
code /
prosody
Comparison
util/termcolours.lua @ 7203:be8b88ad35a3
util.termcolours: Add 256 color support
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 27 Feb 2016 16:47:12 +0100 |
parent | 7199:5846e0bca4ff |
child | 7204:ab2b7496a617 |
comparison
equal
deleted
inserted
replaced
7202:5bf0ff3882aa | 7203:be8b88ad35a3 |
---|---|
12 local t_concat, t_insert = table.concat, table.insert; | 12 local t_concat, t_insert = table.concat, table.insert; |
13 local char, format = string.char, string.format; | 13 local char, format = string.char, string.format; |
14 local tonumber = tonumber; | 14 local tonumber = tonumber; |
15 local ipairs = ipairs; | 15 local ipairs = ipairs; |
16 local io_write = io.write; | 16 local io_write = io.write; |
17 local m_floor = math.floor; | |
17 | 18 |
18 local windows; | 19 local windows; |
19 if os.getenv("WINDIR") then | 20 if os.getenv("WINDIR") then |
20 windows = require "util.windows"; | 21 windows = require "util.windows"; |
21 end | 22 end |
52 return format(fmt_string, style, text); | 53 return format(fmt_string, style, text); |
53 else | 54 else |
54 return text; | 55 return text; |
55 end | 56 end |
56 end | 57 end |
58 | |
59 local function gray(n) | |
60 return m_floor(n*3/32)+0xe8; | |
61 end | |
62 local function color(r,g,b) | |
63 if r == g and g == b then | |
64 return gray(r); | |
65 end | |
66 r = m_floor(r*3/128); | |
67 g = m_floor(g*3/128); | |
68 b = m_floor(b*3/128); | |
69 return 0x10 + ( r * 36 ) + ( g * 6 ) + ( b ); | |
70 end | |
71 local function hex2rgb(hex) | |
72 local r = tonumber(hex:sub(1,2),16); | |
73 local g = tonumber(hex:sub(3,4),16); | |
74 local b = tonumber(hex:sub(5,6),16); | |
75 return r,g,b; | |
76 end | |
77 | |
78 setmetatable(stylemap, { __index = function(_, style) | |
79 local g = style:sub(7) == " background" and "48;5;" or "38;5;"; | |
80 return g .. color(hex2rgb(style)); | |
81 end } ); | |
57 | 82 |
58 local function getstyle(...) | 83 local function getstyle(...) |
59 local styles, result = { ... }, {}; | 84 local styles, result = { ... }, {}; |
60 for i, style in ipairs(styles) do | 85 for i, style in ipairs(styles) do |
61 style = stylemap[style]; | 86 style = stylemap[style]; |