Move color mapping to themes

pull/1/head
cpsdqs 7 years ago
parent 3cf83d0fc5
commit 394654099a
Signed by untrusted user: cpsdqs
GPG Key ID: 3F59586BB7448DD1
  1. 24
      js/term/screen_renderer.js
  2. 31
      js/term/themes.js

@ -1,7 +1,7 @@
const { const {
themes, themes,
buildColorTable, buildColorTable,
SELECTION_FG, SELECTION_BG getColor
} = require('./themes') } = require('./themes')
const { const {
@ -118,27 +118,7 @@ module.exports = class ScreenRenderer {
* @returns {string} the CSS color * @returns {string} the CSS color
*/ */
getColor (i) { getColor (i) {
// return palette color if it exists return getColor(i, this.palette)
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'
} }
/** /**

@ -73,7 +73,7 @@ exports.buildColorTable = function () {
if (colorTable256 !== null) return colorTable256 if (colorTable256 !== null) return colorTable256
// 256color lookup table // 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') colorTable256 = new Array(16).fill('#000000')
// fill color table // 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) { exports.toHex = function (shade, themeN) {
if (/^\d+$/.test(shade)) { if (/^\d+$/.test(shade)) {
shade = +shade shade = +shade

Loading…
Cancel
Save