Module: ColorAdjuster

Defined in:
lib/color_adjuster.rb,
lib/color_adjuster/version.rb

Overview

Include this module in a class that responds to #color and returns a hex string

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#brightnessObject



27
28
29
# File 'lib/color_adjuster.rb', line 27

def brightness
  color.scan(/../).map(&:hex).inject(0) { |sum, i| sum += i }
end

#contrasting_colorObject



23
24
25
# File 'lib/color_adjuster.rb', line 23

def contrasting_color
  brightness > 382.5 ? darken_color(0.2) : lighten_color(0.6)
end

#darken_color(amount = 0.4) ⇒ Object

Amount should be a decimal between 0 and 1. Lower means darker



6
7
8
9
10
11
12
# File 'lib/color_adjuster.rb', line 6

def darken_color(amount = 0.4)
  rgb = color.gsub('#','').scan(/../).map(&:hex)
  rgb[0] = (rgb[0].to_i * amount).round
  rgb[1] = (rgb[1].to_i * amount).round
  rgb[2] = (rgb[2].to_i * amount).round
  "#%02x%02x%02x" % rgb
end

#lighten_color(amount = 0.3) ⇒ Object

Amount should be a decimal between 0 and 1. Higher means lighter



15
16
17
18
19
20
21
# File 'lib/color_adjuster.rb', line 15

def lighten_color(amount = 0.3)
  rgb = color.gsub('#','').scan(/../).map(&:hex)
  rgb[0] = [(rgb[0].to_i + 255 * amount).round, 255].min
  rgb[1] = [(rgb[1].to_i + 255 * amount).round, 255].min
  rgb[2] = [(rgb[2].to_i + 255 * amount).round, 255].min
  "#%02x%02x%02x" % rgb
end