diff options
Diffstat (limited to 'js/vendor/colorcode.js')
| -rw-r--r-- | js/vendor/colorcode.js | 225 |
1 files changed, 157 insertions, 68 deletions
diff --git a/js/vendor/colorcode.js b/js/vendor/colorcode.js index c401b84..07f93bf 100644 --- a/js/vendor/colorcode.js +++ b/js/vendor/colorcode.js @@ -2,13 +2,14 @@ var colorcode = {}; module.exports = colorcode; colorcode.to_json = require('./src/to_json'); +colorcode.to_ansi = require('./src/to_ansi'); colorcode.from_json = require('./src/from_json'); colorcode.style = require('./src/style'); colorcode.to_canvas = require('./src/canvas'); colorcode.color = require('./src/color'); colorcode.font = require('./src/font'); -},{"./src/canvas":2,"./src/color":3,"./src/font":4,"./src/from_json":8,"./src/style":9,"./src/to_json":10}],2:[function(require,module,exports){ +},{"./src/canvas":2,"./src/color":3,"./src/font":4,"./src/from_json":8,"./src/style":9,"./src/to_ansi":10,"./src/to_json":11}],2:[function(require,module,exports){ var to_json = require('./to_json'); var fontload = require('./font').load; var style = require('./style'); @@ -114,7 +115,7 @@ var to_canvas = function(string_or_json, opts){ module.exports = to_canvas; -},{"./color":3,"./font":4,"./style":9,"./to_json":10,"canvas":11}],3:[function(require,module,exports){ +},{"./color":3,"./font":4,"./style":9,"./to_json":11,"canvas":12}],3:[function(require,module,exports){ var style = require('./style'); var color = {}; @@ -238,11 +239,12 @@ if (typeof document === "undefined") font.img_path = __dirname + "/../examples/w font.list = {}; -font.list.fixedsys = require('./font/fixedsys'); +var fsexps = require('./font/fixedsys'); var cp437s = require('./font/cp437'); +for (f in fsexps) font.list[fsexps[f].name] = fsexps[f]; for (f in cp437s) font.list[cp437s[f].name] = cp437s[f]; -style.font = 'fixedsys'; +style.font = 'fixedsys_8x16'; var err_font_load = function(){ console.log("couldn't load font") @@ -270,7 +272,7 @@ font.load = function(font_name, callback_fn){ } -},{"./font/cp437":5,"./font/fixedsys":6,"./style":9,"canvas":11}],5:[function(require,module,exports){ +},{"./font/cp437":5,"./font/fixedsys":6,"./style":9,"canvas":12}],5:[function(require,module,exports){ var cp437s = [[8,8],[8,12],[8,14],[8,16],[10,10],[10,16],[12,12],[16,16]] var fonts = {}; module.exports = fonts; @@ -306,39 +308,49 @@ for (var i=0, wh; wh=cp437s[i]; i++){ // window.cp437 = cp437; },{"../fontutil":7}],6:[function(require,module,exports){ -var font = {}; -module.exports = font; -font.name = 'fixedsys' -font.sheet_url = './img/fsex-simple.png' -font.sheet_w_in_chars = 128 -//url_sheet = 'http://i.imgur.com/vSzBNwZ.png' -font.char_w = 8 -font.char_h = 16; -font.is_char_blank = require('../fontutil').is_char_blank; +var fsexps = [[8,16,0],[8,15,1]] +var fonts = {}; +module.exports = fonts; -font.render_char = function(font, char_value, ctx, ctx_x, ctx_y, char){ +var render_char = function(font, char_value, ctx, ctx_x, ctx_y, char){ var sheet_x = 0, sheet_y = 3; if (char_value >= 0x20 && char_value <= 0x7e){ // ascii - sheet_x = (char_value - 0x20) * font.char_w + sheet_x = (char_value - 0x20) * font.char_w_sheet if (char.i){ // italic - sheet_y = 1 * font.char_h + 3 + sheet_y = 1 * font.char_h_sheet + 3 } } else if (char_value >= 0x2500 && char_value <= 0x25ff){ // geom - sheet_x = (char_value - 0x2500) * font.char_w; - sheet_y = 2 * font.char_h + 3 + sheet_x = (char_value - 0x2500) * font.char_w_sheet; + sheet_y = 2 * font.char_h_sheet + 3 } else if (char_value >= 0x2600 && char_value <= 0x26ff){ // emoji - sheet_x = (char_value - 0x2600) * font.char_w; - sheet_y = 3 * font.char_h + 3 + sheet_x = (char_value - 0x2600) * font.char_w_sheet; + sheet_y = 3 * font.char_h_sheet + 3 } // var sheet_x = (char_value % font.sheet_w_in_chars) * font.char_w // var sheet_y = ((char_value / font.sheet_w_in_chars) |0) * font.char_h + 3 ctx.drawImage(font.sheet, - sheet_x|0, sheet_y|0, font.char_w, font.char_h, + sheet_x|0, (sheet_y|0) + font.y_adj, font.char_w, font.char_h, ctx_x|0, ctx_y|0, font.char_w, font.char_h) } +for (var i=0, wh; wh=fsexps[i]; i++){ + var font = { + name: 'fixedsys_' + wh[0] + 'x' + wh[1], + sheet_url: './img/fsex-simple.png', + sheet_w_in_chars: 128, + char_w_sheet: 8, + char_h_sheet: 16, + char_w: wh[0], + char_h: wh[1], + y_adj: wh[2], + is_char_blank: require('../fontutil').is_char_blank, + render_char: render_char + } + fonts[font.name] = font +} + },{"../fontutil":7}],7:[function(require,module,exports){ var util = {}; module.exports = util; @@ -389,14 +401,79 @@ var style = {}; module.exports = style; },{}],10:[function(require,module,exports){ -var default_style = require('./style'); +var to_json = require('./to_json') +var color = require('./color') + +var palette = color.palettes.mirc + +/** + * Translates a 255 RGB value to a 0-5 ANSI RGV value, + * then returns the single ANSI color code to use. + */ + +function rgb (r, g, b) { + var red = Number(r) / 255 * 5 + , green = Number(g) / 255 * 5 + , blue = Number(b) / 255 * 5 + return rgb5(red, green, blue) +} + +/** + * Turns rgb 0-5 values into a single ANSI color code to use. + */ + +function rgb5 (r, g, b) { + var red = Math.round(r) + , green = Math.round(g) + , blue = Math.round(b) + return 16 + (red*36) + (green*6) + blue +} + +var palette_ansi = [] + +for (var i=0, rgb_css; rgb_css = palette[i]; i++){ + var rgbs = rgb_css + .replace('rgb(', '') + .replace(')', '') + .split(','); + var rgb_ansi = '5;' + rgb(rgbs[0],rgbs[1],rgbs[2]) + palette_ansi.push(rgb_ansi); +} + +var char_escape = '\x1b['; +// var char_escape = '\x1b['; +var char_reset = char_escape + '0m'; + +var mirc_color_to_ansi_color = [ + +] + +var colorcode_to_ansi = function(string_or_json, opts){ + opts = opts || {}; + + var json = to_json(string_or_json, opts); -default_style.b = false; -default_style.i = false; -default_style.u = false; -default_style.fg = 1; -default_style.bg = 99; + var out = ""; + + for (var l = 0, line; line = json.lines[l]; l++){ + for (var c = 0, char; char = line[c]; c++){ + out += char_escape; + out += '38;' + palette_ansi[char.fg] + out += ';' + out += '48;' + palette_ansi[char.bg] + out += 'm' + out += String.fromCharCode(char.value); + } + out += char_reset + '\n'; + } + + return out; +} + +module.exports = colorcode_to_ansi; + +},{"./color":3,"./to_json":11}],11:[function(require,module,exports){ var char_color = '\x03'; var regexp_color = /(^[\d]{1,2})?(?:,([\d]{1,2}))?/; @@ -409,60 +486,77 @@ var style_chars = { }; var Style = function(style){ - this.b = style.b - this.i = style.i - this.u = style.u - this.fg = style.fg - this.bg = style.bg + this.b = style.b; + this.i = style.i; + this.u = style.u; + this.fg = style.fg; + this.bg = style.bg; }; var style_fns = {}; -style_fns.bold = function(style){ style.b = !style.b } +style_fns.bold = function(style){ style.b = !style.b }; -style_fns.italic = function(style){ style.i = !style.i } +style_fns.italic = function(style){ style.i = !style.i }; -style_fns.underline = function(style){ style.u = !style.u } +style_fns.underline = function(style){ style.u = !style.u }; style_fns.inverse = function(style){ var tmp = style.fg; style.fg = style.bg; style.bg = tmp; -} +}; style_fns.reset = function(style, base_style){ - style.b = base_style.b - style.i = base_style.i - style.u = base_style.u - style.fg = base_style.fg - style.bg = base_style.bg -} + style.b = base_style.b; + style.i = base_style.i; + style.u = base_style.u; + style.fg = base_style.fg; + style.bg = base_style.bg; +}; + +var colorcode_to_json = function(string, opts){ + // looks like its already converted + if (typeof string === 'object' && + 'lines' in string && + 'w' in string && + 'h' in string) + return string; + -var to_json = function(string, opts){ opts = opts || {}; + var d = colorcode_to_json.defaults; var base_style = { - "b": ("b" in opts) ? opts.b : default_style.b, - "i": ("i" in opts) ? opts.i : default_style.i, - "u": ("u" in opts) ? opts.u : default_style.u, - "fg": ("fg" in opts) ? opts.fg : default_style.fg, - "bg": ("bg" in opts) ? opts.bg : default_style.bg + b: "b" in opts ? opts.b : d.b, + i: "i" in opts ? opts.i : d.i, + u: "u" in opts ? opts.u : d.u, + fg: "fg" in opts ? opts.fg : d.fg, + bg: "bg" in opts ? opts.bg : d.bg }; var lines_in = string.split(/\r?\n/); - var data = []; + var lines_out = []; var w = 0, h = 0; for (var i=0; i<lines_in.length; i++){ var line = lines_in[i]; - if (line.length === 0) continue; + if (line.length === 0) continue; // skip blank lines var json_line = line_to_json(line, base_style); if (w < json_line.length) w = json_line.length; - data.push(json_line); + lines_out.push(json_line); h++; } - return {w:w, h:h, lines:data}; + return {w:w, h:h, lines:lines_out}; +}; + +colorcode_to_json.defaults = { + b: false +, i: false +, u: false +, fg: 1 +, bg: 99 }; var line_to_json = function(line, base_style){ @@ -493,13 +587,11 @@ var line_to_json = function(line, base_style){ continue; } - if (matches[1] !== undefined){ - style.fg = Number(matches[1]) - } + if (matches[1] !== undefined) + style.fg = Number(matches[1]); - if (matches[2] !== undefined){ - style.bg = Number(matches[2]) - } + if (matches[2] !== undefined) + style.bg = Number(matches[2]); pos += matches[0].length; continue; @@ -509,19 +601,16 @@ var line_to_json = function(line, base_style){ // otherwise, next char is treated as normal content var data = new Style(style); //data.value = char; - data.value = char.charCodeAt(0) + data.value = char.charCodeAt(0); - out.push(data) + out.push(data); } - return out -} - - - -module.exports = to_json; + return out; +}; +module.exports = colorcode_to_json; -},{"./style":9}],11:[function(require,module,exports){ +},{}],12:[function(require,module,exports){ },{}]},{},[1]) (1) |
