From 394654099a066427fdde54b48b7bab7179ca8198 Mon Sep 17 00:00:00 2001 From: cpsdqs Date: Sun, 8 Oct 2017 11:55:47 +0200 Subject: [PATCH] Move color mapping to themes --- js/term/screen_renderer.js | 24 ++---------------------- js/term/themes.js | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/js/term/screen_renderer.js b/js/term/screen_renderer.js index 0a174bd..1f935d2 100644 --- a/js/term/screen_renderer.js +++ b/js/term/screen_renderer.js @@ -1,7 +1,7 @@ const { themes, buildColorTable, - SELECTION_FG, SELECTION_BG + getColor } = require('./themes') const { @@ -118,27 +118,7 @@ module.exports = class ScreenRenderer { * @returns {string} the CSS color */ getColor (i) { - // return palette color if it exists - if (i < 16 && i in this.palette) return this.palette[i] - - // -1 for selection foreground, -2 for selection background - if (i === -1) return SELECTION_FG - if (i === -2) return SELECTION_BG - - // 256 color - if (i > 15 && i < 256) return this.colorTable256[i] - - // true color, encoded as (hex) + 256 (such that #000 == 256) - if (i > 255) { - i -= 256 - let red = (i >> 16) & 0xFF - let green = (i >> 8) & 0xFF - let blue = i & 0xFF - return `rgb(${red}, ${green}, ${blue})` - } - - // return error color - return (Date.now() / 1000) % 2 === 0 ? '#f0f' : '#0f0' + return getColor(i, this.palette) } /** diff --git a/js/term/themes.js b/js/term/themes.js index 8fb47ac..5c65533 100644 --- a/js/term/themes.js +++ b/js/term/themes.js @@ -73,7 +73,7 @@ exports.buildColorTable = function () { if (colorTable256 !== null) return colorTable256 // 256color lookup table - // should not be used to look up 0-15 (will return transparent) + // should not be used to look up 0-15 colorTable256 = new Array(16).fill('#000000') // fill color table @@ -113,6 +113,35 @@ exports.themePreview = function (themeN) { }) } +exports.colorTable256 = null +exports.ensureColorTable256 = function () { + if (!exports.colorTable256) exports.colorTable256 = exports.buildColorTable() +} + +exports.getColor = function (i, palette = []) { + // return palette color if it exists + if (i < 16 && i in palette) return palette[i] + + // -1 for selection foreground, -2 for selection background + if (i === -1) return exports.SELECTION_FG + if (i === -2) return exports.SELECTION_BG + + // 256 color + if (i > 15 && i < 256) { + exports.ensureColorTable256() + return exports.colorTable256[i] + } + + // 24-bit color, encoded as (hex) + 256 (such that #000000 == 256) + if (i > 255) { + i -= 256 + return '#' + `000000${i.toString(16)}`.substr(-6) + } + + // return error color + return Math.floor(Date.now() / 1000) % 2 === 0 ? '#f0f' : '#0f0' +} + exports.toHex = function (shade, themeN) { if (/^\d+$/.test(shade)) { shade = +shade