Support RGB color in thumbnails

ex-upload
Marcin Kulik 7 years ago
parent ec794000ed
commit 8c63169607

@ -9,14 +9,32 @@ class BrushDecorator < ApplicationDecorator
end
end
def css_style
attrs = {}
if Brush.rgb_color?(model.fg)
r, g, b = model.fg
attrs['color'] = "rgb(#{r},#{b},#{g})"
end
if Brush.rgb_color?(model.bg)
r, g, b = model.bg
attrs['background-color'] = "rgb(#{r},#{b},#{g})"
end
if !attrs.empty?
attrs.reduce("") { |acc, kv| acc + "#{kv[0]}:#{kv[1]};" }
end
end
private
def fg_class
"fg-#{model.fg}" if model.fg
"fg-#{model.fg}" if model.fg && !Brush.rgb_color?(model.fg)
end
def bg_class
"bg-#{model.bg}" if model.bg
"bg-#{model.bg}" if model.bg && !Brush.rgb_color?(model.bg)
end
def bold_class

@ -2,6 +2,7 @@ class CellDecorator < ApplicationDecorator
delegate_all
delegate :css_class, to: :brush
delegate :css_style, to: :brush
def brush
BrushDecorator.new(model.brush)

@ -48,6 +48,14 @@ class Brush
attributes.slice(*ALLOWED_ATTRIBUTES)
end
def self.rgb_color?(col)
col.is_a?(Enumerable)
end
def self.simple_color?(col)
col.is_a?(Fixnum)
end
protected
attr_reader :attributes
@ -65,10 +73,8 @@ class Brush
def calculate_code(attr_name, strong)
code = attributes[attr_name]
if code
if code < 8 && strong
code += 8
end
if Brush.simple_color?(code) && code < 8 && strong
code += 8
end
code

@ -2,5 +2,5 @@ pre.asciinema-terminal.font-small
- for line in thumbnail.lines
span.line
- for cell in line
span class=cell.css_class = cell.text
span class=cell.css_class style=cell.css_style = cell.text
= "\n"

@ -29,7 +29,7 @@ describe BrushDecorator do
it { should_not match(/\bfg/) }
end
context "when fg is non-default" do
context "when fg is non-default (number)" do
before do
allow(brush).to receive(:fg) { 1 }
end
@ -37,6 +37,14 @@ describe BrushDecorator do
it { should match(/\bfg-1\b/) }
end
context "when fg is non-default (rgb)" do
before do
allow(brush).to receive(:fg) { [1, 2, 3] }
end
it { should eq("") }
end
context "when bg is default" do
before do
allow(brush).to receive(:bg) { nil }
@ -45,7 +53,7 @@ describe BrushDecorator do
it { should_not match(/\bbg/) }
end
context "when bg is non-default" do
context "when bg is non-default (number)" do
before do
allow(brush).to receive(:bg) { 2 }
end
@ -53,6 +61,14 @@ describe BrushDecorator do
it { should match(/\bbg-2\b/) }
end
context "when bg is non-default (rgb)" do
before do
allow(brush).to receive(:bg) { [1, 2, 3] }
end
it { should eq("") }
end
context "when both fg and bg are non-default" do
before do
allow(brush).to receive(:fg) { 1 }
@ -80,4 +96,54 @@ describe BrushDecorator do
end
end
end
describe '#css_style' do
subject { decorator.css_style }
context "when brush is a default one" do
before do
allow(brush).to receive(:default?) { true }
end
it { should be(nil) }
end
context "when brush is not a default one" do
before do
allow(brush).to receive(:default?) { false }
end
context "when fg is non-default (number)" do
before do
allow(brush).to receive(:fg) { 1 }
end
it { should be(nil) }
end
context "when fg is non-default (rgb)" do
before do
allow(brush).to receive(:fg) { [229, 222, 19] }
end
it { should match(/color:rgb\(\d+,\d+,\d+\)/) }
end
context "when bg is non-default (number)" do
before do
allow(brush).to receive(:bg) { 2 }
end
it { should be(nil) }
end
context "when bg is non-default (rgb)" do
before do
allow(brush).to receive(:bg) { [111, 222, 255] }
end
it { should match(/background-color:rgb\(\d+,\d+,\d+\)/) }
end
end
end
end

@ -60,6 +60,10 @@ describe Brush do
expect(Brush.new(bg: 0, inverse: true, blink: true).fg).to eq(8)
expect(Brush.new(inverse: true, blink: true).fg).to eq(0)
end
it 'supports rgb color' do
expect(Brush.new(fg: [229, 222, 19]).fg).to eq([229, 222, 19])
end
end
describe '#bg' do

Loading…
Cancel
Save