class PRRD::Color

Color class

Attributes

collection[RW]

Accessors

hexcode[RW]

Accessors

Public Class Methods

new(name, tint = nil, alpha = nil) click to toggle source

Constructor @param name [Symbol] @param tint [Symbol, nil] @param alpha [Symbol, nil]

# File lib/prrd.rb, line 61
def initialize(name, tint = nil, alpha = nil)
  if name.is_a?(String) && name.include?('#')
    @hexcode = name
  else
    @hexcode = to_hex(name, tint, alpha)
  end
end

Public Instance Methods

darken(percentage) click to toggle source

Darken the color @param percentage [Integer] @return [String]

# File lib/prrd.rb, line 99
def darken(percentage)
  fail 'Can not operate on a non-hexadecimal color' if @hexcode.nil?

  hexcode = @hexcode.gsub('#', '')
  percentage = percentage.to_f / 100
  rgb = hexcode.scan(/../).map { |c| c.hex }
  rgb.map! { |c| (c.to_i * percentage).round }

  "#%02x%02x%02x" % rgb
end
lighten(percentage) click to toggle source

Lighten the color @param percentage [Integer]

# File lib/prrd.rb, line 112
def lighten(percentage)
  fail 'Can not operate on a non-hexadecimal color' if @hexcode.nil?

  hexcode = @hexcode.gsub('#', '')
  percentage = percentage.to_f / 100
  rgb = hexcode.scan(/../).map { |c| c.hex }
  rgb.map! { |c| [(c.to_i + 255 * percentage).round, 255].min }

  "#%02x%02x%02x" % rgb
end
to_hex(name, tint = nil, alpha = nil) click to toggle source

Translate into hexcode @param name [Symbol] @param tint [Symbol, nil] @param alpha [Symbol, nil] @param [String]

# File lib/prrd.rb, line 74
def to_hex(name, tint = nil, alpha = nil)
  if @collection.nil?
    @collection = {
      red: {light: '#EA644A', dark: '#CC3118'},
      orange: {light: '#EC9D48', dark: '#CC7016'},
      yellow: {light: '#ECD748', dark: '#C9B215'},
      green: {light: '#54EC48', dark: '#24BC14'},
      blue: {light: '#48C4EC', dark: '#1598C3'},
      pink: {light: '#DE48EC', dark: '#B415C7'},
      purple: {light: '#7648EC', dark: '#4D18E4'}
    }
  end

  name = name.to_sym
  tint = tint.nil? ? :light : tint.to_sym

  fail "Unknown color #{name}" unless @collection.key? name
  fail "Unknown tint #{tint}" unless @collection[name].key? tint

  alpha.nil? ? @collection[name][tint] : @collection[name][tint] + alpha
end
to_s() click to toggle source

String representation

# File lib/prrd.rb, line 124
def to_s
  @hexcode
end