Skip to content

Commit

Permalink
corrected handling of rgb colors (foreground and background)
Browse files Browse the repository at this point in the history
There was an off by one error in the use of the regexp match data

fixes issue #4
  • Loading branch information
masukomi committed Sep 4, 2018
1 parent fe29d56 commit beeb5b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions .changelog_entries/e17764deb76cf2cd998f0d5d8e14d6f3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"Deprecated","tickets":["4"],"url":"https://github.com/masukomi/oho/issues/4","description":"corrects detection and handling of rgb color escape sequences","tags":["bug"]}
25 changes: 25 additions & 0 deletions spec/oho/color_escape_code_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,29 @@ describe Oho::ColorEscapeCode do
ec.background_color.should(eq(""))
end

describe "rgb colors" do

it "should recognize rgb foreground colors" do
# breakdown
# normal foreground color codes go from 37 to 39 skipping 38
# 38 is bug unless followed by 5 or 2
# 38;2 -> indicates rgb color is about to follow
# thus rgb should be 252, 0, 37
escape_code_string = "[38;2;252;0;37m" # the \033 or \e will be stripped before
ec = Oho::ColorEscapeCode.new(escape_code_string, default_options)
ec.to_span(nil).should(eq(
"<span style=\"color: rgb(252,0,37); \">"))
end
it "should recognize rgb background colors" do
# breakdown
# normal background color codes go from 47 to 49 skipping 38
# 48 is bug unless followed by 5 or 2
# 48;2 -> indicates rgb color is about to follow
# thus rgb should be 252, 0, 37
escape_code_string = "[48;2;252;0;37m" # the \033 or \e will be stripped before
ec = Oho::ColorEscapeCode.new(escape_code_string, default_options)
ec.to_span(nil).should(eq(
"<span style=\"background-color: rgb(252,0,37); \">"))
end
end
end
8 changes: 7 additions & 1 deletion src/oho/color_escape_code.cr
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ module Oho
private def extract_rgb_color(m : Regex::MatchData?) : String
return "" if m.nil?
rgb = m.as(Regex::MatchData)
"rgb(#{rgb[0]},#{rgb[1]},#{rgb[2]})"
"rgb(#{rgb[1]},#{rgb[2]},#{rgb[3]})"
end

private def extract_foreground_color(string : String) : String
Expand All @@ -557,6 +557,9 @@ module Oho
m = string.match(/38;2;(\d+);(\d+);(\d+)/)
return extract_rgb_color(m) if ! m.nil?
ints = extract_ints(string)
return "" if ints[0] == 48
# 48;2 and 48;5 are background colors
# 48 by itself is a bug
if ints.size > 0 && ints.last == 0
return "" # alternately could "initial"
end
Expand Down Expand Up @@ -586,6 +589,9 @@ module Oho
return extract_rgb_color(m) if ! m.nil?
# who knows
ints = extract_ints(string)
return "" if ints[0] == 38
# 38;2 and 38;5 are background colors
# 38 by itself is a bug
if ints.size > 0 && ints.last == 0
return "" # alternately could "initial"
end
Expand Down

0 comments on commit beeb5b4

Please sign in to comment.