Skip to content

Commit

Permalink
Use less map, mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
flori committed Jul 23, 2024
1 parent 22b47dc commit 5286e55
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/term/ansicolor/hsl_triple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Term
module ANSIColor
class HSLTriple
def self.from_rgb_triple(rgb)
ps = rgb.to_a.map { |x| x / 255.0 }
ps = [ rgb.red / 255.0, rgb.green / 255.0, rgb.blue / 255.0 ]
p_min = ps.min
p_max = ps.max
p_red, p_green, p_blue = ps
Expand Down
10 changes: 5 additions & 5 deletions lib/term/ansicolor/rgb_color_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ def normalize(v)
if v <= 0.04045
v / 12
else
( (v + 0.055) / 1.055) ** 2.4
( (v + 0.055) / 1.055 ) ** 2.4
end
end

def normalize_rgb_triple(rgb_triple)
[
rgb_triple.red,
rgb_triple.green,
rgb_triple.blue
].map { |v| normalize(v) }
normalize(rgb_triple.red),
normalize(rgb_triple.green),
normalize(rgb_triple.blue),
]
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/term/ansicolor/rgb_triple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ def self.[](thing)
end

def initialize(red, green, blue)
@values = [ red, green, blue ].map! { |v| v.clamp(0, 0xff) }
@values = [
red.clamp(0, 0xff),
green.clamp(0, 0xff),
blue.clamp(0, 0xff),
]
end

def red
Expand Down Expand Up @@ -106,7 +110,7 @@ def html

def css(percentage: false)
if percentage
"rgb(%s%%,%s%%,%s%%)" % @values.map { |v| 100.0 * v / 255 }
"rgb(%s%%,%s%%,%s%%)" % percentages
else
"rgb(%u,%u,%u)" % @values
end
Expand Down

0 comments on commit 5286e55

Please sign in to comment.