Skip to content

Commit

Permalink
Add some more codes for text
Browse files Browse the repository at this point in the history
  • Loading branch information
flori committed Jun 21, 2024
1 parent f285f60 commit 2f842da
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 73 deletions.
29 changes: 19 additions & 10 deletions lib/term/ansicolor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def self.true_coloring=(val)

def self.create_color_method(color_name, color_value)
module_eval <<-EOT
def #{color_name}(string = nil, &block)
color(:#{color_name}, string, &block)
def #{color_name}(string = nil, **options, &block)
apply_attribute(:#{color_name}, string, **options, &block)
end
EOT
self
Expand All @@ -79,7 +79,7 @@ def #{color_name}(string = nil, &block)

# Regular expression that is used to scan for ANSI-Attributes while
# uncoloring strings.
COLORED_REGEXP = /\e\[(?:(?:[349]|10)[0-7]|[0-9]|[34]8;(5;\d{1,3}|2;\d{1,3}(;\d{1,3}){2}))?m/
COLORED_REGEXP = /\e\[(?:(?:[349]|10)[0-7]|[0-9]|[34]8;(5;\d{1,3}|2;\d{1,3}(;\d{1,3}){2})|4:\d|53)?m/

# Returns an uncolored version of the string, that is all ANSI-Attributes
# are stripped from the string.
Expand All @@ -97,11 +97,9 @@ def uncolor(string = nil) # :yields:

alias uncolored uncolor

# Return +string+ or the result string of the given +block+ colored with
# color +name+. If string isn't a string only the escape sequence to switch
# on the color +name+ is returned.
def color(name, string = nil, &block)
attribute = Attribute[name] or raise ArgumentError, "unknown attribute #{name.inspect}"
def apply_attribute(name, string = nil, &block)
attribute = Attribute[name] or
raise ArgumentError, "unknown attribute #{name.inspect}"
result = ''
result << "\e[#{attribute.code}m" if Term::ANSIColor.coloring?
if block_given?
Expand All @@ -117,11 +115,22 @@ def color(name, string = nil, &block)
result.extend(Term::ANSIColor)
end

# Return +string+ or the result string of the given +block+ colored with
# color +name+. If string isn't a string only the escape sequence to switch
# on the color +name+ is returned.
def color(name, string = nil, &block)
apply_attribute(name, string, &block)
end

# Return +string+ or the result string of the given +block+ with a
# background colored with color +name+. If string isn't a string only the
# escape sequence to switch on the color +name+ is returned.
def on_color(name, string = nil, &block)
attribute = Attribute[name] or raise ArgumentError, "unknown attribute #{name.inspect}"
attribute = Attribute[name] or
raise ArgumentError, "unknown attribute #{name.inspect}"
attribute = attribute.dup
attribute.background = true
color(attribute, string, &block)
apply_attribute(attribute, string, &block)
end

class << self
Expand Down
5 changes: 3 additions & 2 deletions lib/term/ansicolor/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def self.on_true_color(color, options = {})

def initialize(name, code, options = {})
@name = name.to_sym
@background = @name.start_with?('on_')
@background = !!options[:background]
@code = code.to_s
@true_color = false
if rgb = options[:true_color]
@true_color = true
@rgb = rgb
Expand All @@ -122,7 +123,7 @@ def code
end

def apply(string = nil, &block)
::Term::ANSIColor.color(self, string, &block)
::Term::ANSIColor.apply_attribute(self, string, &block)
end

def background?
Expand Down
74 changes: 38 additions & 36 deletions lib/term/ansicolor/attribute/color256.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,67 @@ module Term
module ANSIColor
class Attribute
class Color256
Attribute.set :color0, 0, :html => '#000000'
Attribute.set :color1, 1, :html => '#800000'
Attribute.set :color2, 2, :html => '#808000'
Attribute.set :color3, 3, :html => '#808000'
Attribute.set :color4, 4, :html => '#000080'
Attribute.set :color5, 5, :html => '#800080'
Attribute.set :color6, 6, :html => '#008080'
Attribute.set :color7, 7, :html => '#c0c0c0'
Attribute.set :color0, 0, html: '#000000'
Attribute.set :color1, 1, html: '#800000'
Attribute.set :color2, 2, html: '#808000'
Attribute.set :color3, 3, html: '#808000'
Attribute.set :color4, 4, html: '#000080'
Attribute.set :color5, 5, html: '#800080'
Attribute.set :color6, 6, html: '#008080'
Attribute.set :color7, 7, html: '#c0c0c0'

Attribute.set :color8, 8, :html => '#808080'
Attribute.set :color9, 9, :html => '#ff0000'
Attribute.set :color10, 10, :html => '#00ff00'
Attribute.set :color11, 11, :html => '#ffff00'
Attribute.set :color12, 12, :html => '#0000ff'
Attribute.set :color13, 13, :html => '#ff00ff'
Attribute.set :color14, 14, :html => '#00ffff'
Attribute.set :color15, 15, :html => '#ffffff'
Attribute.set :color8, 8, html: '#808080'
Attribute.set :color9, 9, html: '#ff0000'
Attribute.set :color10, 10, html: '#00ff00'
Attribute.set :color11, 11, html: '#ffff00'
Attribute.set :color12, 12, html: '#0000ff'
Attribute.set :color13, 13, html: '#ff00ff'
Attribute.set :color14, 14, html: '#00ffff'
Attribute.set :color15, 15, html: '#ffffff'

steps = [ 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff ]

for i in 16..231
red, green, blue = (i - 16).to_s(6).rjust(3, '0').each_char.map { |c| steps[c.to_i] }
Attribute.set "color#{i}", i, :red => red, :green => green, :blue => blue
Attribute.set "color#{i}", i, red: red, green: green, blue: blue
end

grey = 8
for i in 232..255
Attribute.set "color#{i}", i, :red => grey, :green => grey, :blue => grey
Attribute.set "color#{i}", i, red: grey, green: grey, blue: grey
grey += 10
end

Attribute.set :on_color0, 0, :html => '#000000'
Attribute.set :on_color1, 1, :html => '#800000'
Attribute.set :on_color2, 2, :html => '#808000'
Attribute.set :on_color3, 3, :html => '#808000'
Attribute.set :on_color4, 4, :html => '#000080'
Attribute.set :on_color5, 5, :html => '#800080'
Attribute.set :on_color6, 6, :html => '#008080'
Attribute.set :on_color7, 7, :html => '#c0c0c0'
Attribute.set :on_color0, 0, html: '#000000', background: true
Attribute.set :on_color1, 1, html: '#800000', background: true
Attribute.set :on_color2, 2, html: '#808000', background: true
Attribute.set :on_color3, 3, html: '#808000', background: true
Attribute.set :on_color4, 4, html: '#000080', background: true
Attribute.set :on_color5, 5, html: '#800080', background: true
Attribute.set :on_color6, 6, html: '#008080', background: true
Attribute.set :on_color7, 7, html: '#c0c0c0'

Attribute.set :on_color8, 8, :html => '#808080'
Attribute.set :on_color9, 9, :html => '#ff0000'
Attribute.set :on_color10, 10, :html => '#00ff00'
Attribute.set :on_color11, 11, :html => '#ffff00'
Attribute.set :on_color12, 12, :html => '#0000ff'
Attribute.set :on_color13, 13, :html => '#ff00ff'
Attribute.set :on_color14, 14, :html => '#00ffff'
Attribute.set :on_color15, 15, :html => '#ffffff'
Attribute.set :on_color8, 8, html: '#808080', background: true
Attribute.set :on_color9, 9, html: '#ff0000', background: true
Attribute.set :on_color10, 10, html: '#00ff00', background: true
Attribute.set :on_color11, 11, html: '#ffff00', background: true
Attribute.set :on_color12, 12, html: '#0000ff', background: true
Attribute.set :on_color13, 13, html: '#ff00ff', background: true
Attribute.set :on_color14, 14, html: '#00ffff', background: true
Attribute.set :on_color15, 15, html: '#ffffff', background: true

steps = [ 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff ]

for i in 16..231
red, green, blue = (i - 16).to_s(6).rjust(3, '0').each_char.map { |c| steps[c.to_i] }
Attribute.set "on_color#{i}", i, :red => red, :green => green, :blue => blue
Attribute.set "on_color#{i}", i,
red: red, green: green, blue: blue, background: true
end

grey = 8
for i in 232..255
Attribute.set "on_color#{i}", i, :red => grey, :green => grey, :blue => grey
Attribute.set "on_color#{i}", i,
red: grey, green: grey, blue: grey, background: true
grey += 10
end
end
Expand Down
31 changes: 16 additions & 15 deletions lib/term/ansicolor/attribute/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ module Term
module ANSIColor
class Attribute
class Text
Attribute.set :clear, 0 # String#clear already used in String
Attribute.set :reset, 0 # synonym for :clear
Attribute.set :bold, 1
Attribute.set :dark, 2
Attribute.set :faint, 2
Attribute.set :italic, 3 # not widely implemented
Attribute.set :underline, 4
Attribute.set :underscore, 4 # synonym for :underline
Attribute.set :blink, 5
Attribute.set :rapid_blink, 6 # not widely implemented
Attribute.set :reverse, 7 # String#reverse already used in String
Attribute.set :negative, 7 # synonym for :reverse
Attribute.set :concealed, 8
Attribute.set :conceal, 8 # synonym for :concealed
Attribute.set :strikethrough, 9 # not widely implemented
Attribute.set :clear, 0 # String#clear already used in String
Attribute.set :reset, 0 # synonym for :clear
Attribute.set :bold, 1
Attribute.set :dark, 2
Attribute.set :faint, 2
Attribute.set :italic, 3 # not widely implemented
Attribute.set :underline, 4
Attribute.set :underscore, 4 # synonym for :underline
Attribute.set :blink, 5
Attribute.set :rapid_blink, 6 # not widely implemented
Attribute.set :reverse, 7 # String#reverse already used in String
Attribute.set :negative, 7 # synonym for :reverse
Attribute.set :concealed, 8
Attribute.set :conceal, 8 # synonym for :concealed
Attribute.set :strikethrough, 9 # not widely implemented
Attribute.set :overline, 53 # not widely implemented
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions tests/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ def test_distance_to
assert_in_delta 250.954, Attribute.get(:color0).distance_to(color), 1e-3
color = Attribute.nearest_rgb_color('#0f0')
assert_in_delta 255, Attribute.get(:color0).distance_to(color,
:metric => RGBColorMetrics::Euclidean), 1e-3
metric: RGBColorMetrics::Euclidean), 1e-3
assert_equal 1 / 0.0, Attribute.get(:color0).distance_to(nil)
end

def test_nearest_rgb_color
assert_equal Attribute.get(:color0).rgb, Attribute.nearest_rgb_color('#000').rgb
assert_equal Attribute.get(:color15).rgb, Attribute.nearest_rgb_color('#ffffff').rgb
assert_equal :color248, Attribute.nearest_rgb_color('#aaa').name
assert_equal :color109, Attribute.nearest_rgb_color('#aaa', :gray => false).name
assert_equal :color109, Attribute.nearest_rgb_color('#aaa', gray: false).name
end

def test_nearest_rgb_on_color
assert_equal Attribute.get(:on_color0).rgb, Attribute.nearest_rgb_on_color('#000').rgb
assert_equal Attribute.get(:on_color15).rgb, Attribute.nearest_rgb_on_color('#ffffff').rgb
assert_equal :on_color248, Attribute.nearest_rgb_on_color('#aaa').name
assert_equal :on_color109, Attribute.nearest_rgb_on_color('#aaa', :gray => false).name
assert_equal :on_color109, Attribute.nearest_rgb_on_color('#aaa', gray: false).name
end

def test_apply
Expand All @@ -59,13 +59,13 @@ def test_apply
def test_gradient
g0 = Attribute[:blink].gradient_to Attribute['#30ffaa']
assert_equal [], g0
g1 = Attribute['#30ffaa'].gradient_to(Attribute['#ff507f'], :steps => 9)
g1 = Attribute['#30ffaa'].gradient_to(Attribute['#ff507f'], steps: 9)
assert_equal [ :color49, :color49, :color43, :color79, :color108,
:color247, :color138, :color168, :color204 ], g1.map(&:name)
g2 = Attribute['#30ffaa'].gradient_to(
Attribute['#ff507f'],
:steps => 9,
:metric => RGBColorMetrics::Euclidean
steps: 9,
metric: RGBColorMetrics::Euclidean
)
assert_equal [ :color49, :color43, :color79, :color73, :color108,
:color247, :color138, :color168, :color204 ], g2.map(&:name)
Expand Down Expand Up @@ -93,17 +93,17 @@ def test_true_color_gradient
assert_equal [], g0
g1 = Attribute['#30ffaa'].gradient_to(
Attribute['#ff507f'],
:steps => 9,
steps: 9,
true_coloring: true
)
assert_equal %w[
#00ffaf #1febaa #3ed7a5 #5dc3a0 #7caf9b #9b9b96 #ba8791 #d9738c #ff5f87
], g1.map { _1.rgb.html }
g2 = Attribute['#30ffaa'].gradient_to(
Attribute['#ff507f'],
:steps => 9,
steps: 9,
true_coloring: true,
:metric => RGBColorMetrics::Euclidean
metric: RGBColorMetrics::Euclidean
)
assert_equal %w[
#00ffaf #1febaa #3ed7a5 #5dc3a0 #7caf9b #9b9b96 #ba8791 #d9738c #ff5f87
Expand Down
2 changes: 1 addition & 1 deletion tests/hsl_triple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setup

def test_hsl_cast
assert_equal '#85e085', HSLTriple[ @pastel_green_hsl ].html
assert_equal '#85e085', HSLTriple[ :hue => 120, :saturation => 59.4, :lightness => 70 ].html
assert_equal '#85e085', HSLTriple[ hue: 120, saturation: 59.4, lightness: 70 ].html
assert_equal '#11ddff', HSLTriple[ '#1df' ].html
assert_equal '#8000ff', HSLTriple[ 'rgb(128,0,255)' ].html
assert_equal '#85e085', HSLTriple[ 'hsl(120.0,59.4%,70.0%)' ].html
Expand Down

0 comments on commit 2f842da

Please sign in to comment.