mirror of
https://github.com/rastapasta/mapscii.git
synced 2025-02-16 09:29:13 +01:00
🎨 adding support for background color
This commit is contained in:
parent
64b03e3422
commit
bfe6abadd7
@ -19,10 +19,10 @@ module.exports = class BrailleBuffer
|
|||||||
|
|
||||||
pixelBuffer: null
|
pixelBuffer: null
|
||||||
charBuffer: null
|
charBuffer: null
|
||||||
colorBuffer: null
|
foregroundBuffer: null
|
||||||
|
backgroundBuffer: null
|
||||||
|
|
||||||
termReset: "\x1B[39m"
|
termReset: "\x1B[39;49m"
|
||||||
termColor: (color) -> "\x1B[38;5;#{color}m"
|
|
||||||
|
|
||||||
constructor: (@width, @height) ->
|
constructor: (@width, @height) ->
|
||||||
@pixelBuffer = new Buffer @width*@height/8
|
@pixelBuffer = new Buffer @width*@height/8
|
||||||
@ -31,12 +31,16 @@ module.exports = class BrailleBuffer
|
|||||||
clear: ->
|
clear: ->
|
||||||
@pixelBuffer.fill 0
|
@pixelBuffer.fill 0
|
||||||
@charBuffer = []
|
@charBuffer = []
|
||||||
@colorBuffer = []
|
@foregroundBuffer = []
|
||||||
|
@backgroundBuffer = []
|
||||||
|
|
||||||
|
setBackground: (idx, color) ->
|
||||||
|
@backgroundBuffer[idx] = color
|
||||||
|
|
||||||
setPixel: (x, y, color) ->
|
setPixel: (x, y, color) ->
|
||||||
@_locate x, y, (idx, mask) =>
|
@_locate x, y, (idx, mask) =>
|
||||||
@pixelBuffer[idx] |= mask
|
@pixelBuffer[idx] |= mask
|
||||||
@colorBuffer[idx] = @termColor color
|
@foregroundBuffer[idx] = color
|
||||||
|
|
||||||
unsetPixel: (x, y) ->
|
unsetPixel: (x, y) ->
|
||||||
@_locate x, y, (idx, mask) =>
|
@_locate x, y, (idx, mask) =>
|
||||||
@ -51,6 +55,16 @@ module.exports = class BrailleBuffer
|
|||||||
mask = @characterMap[y&3][x&1]
|
mask = @characterMap[y&3][x&1]
|
||||||
cb idx, mask
|
cb idx, mask
|
||||||
|
|
||||||
|
_termColor: (foreground, background) ->
|
||||||
|
if foreground and background
|
||||||
|
"\x1B[38;5;#{foreground};48;5;#{background}m"
|
||||||
|
else if foreground
|
||||||
|
"\x1B[49;38;5;#{foreground}m"
|
||||||
|
else if background
|
||||||
|
"\x1B[39;48;5;#{background}m"
|
||||||
|
else
|
||||||
|
@termReset
|
||||||
|
|
||||||
frame: ->
|
frame: ->
|
||||||
output = []
|
output = []
|
||||||
currentColor = null
|
currentColor = null
|
||||||
@ -59,7 +73,7 @@ module.exports = class BrailleBuffer
|
|||||||
for idx in [0...@pixelBuffer.length]
|
for idx in [0...@pixelBuffer.length]
|
||||||
output.push delimeter unless idx % (@width/2)
|
output.push delimeter unless idx % (@width/2)
|
||||||
|
|
||||||
if currentColor isnt colorCode = @colorBuffer[idx] or @termReset
|
if currentColor isnt colorCode = @_termColor @foregroundBuffer[idx], @backgroundBuffer[idx]
|
||||||
output.push currentColor = colorCode
|
output.push currentColor = colorCode
|
||||||
|
|
||||||
output.push if @charBuffer[idx]
|
output.push if @charBuffer[idx]
|
||||||
@ -76,7 +90,7 @@ module.exports = class BrailleBuffer
|
|||||||
return unless 0 <= x < @width and 0 <= y < @height
|
return unless 0 <= x < @width and 0 <= y < @height
|
||||||
idx = @_project x, y
|
idx = @_project x, y
|
||||||
@charBuffer[idx] = char
|
@charBuffer[idx] = char
|
||||||
@colorBuffer[idx] = @termColor color
|
@foregroundBuffer[idx] = color
|
||||||
|
|
||||||
writeText: (text, x, y, color, center = true) ->
|
writeText: (text, x, y, color, center = true) ->
|
||||||
x -= text.length/2+1 if center
|
x -= text.length/2+1 if center
|
||||||
|
Loading…
Reference in New Issue
Block a user