Class: StyleTrain::HexColor

Inherits:
ColorType show all
Defined in:
lib/style_train/color_types/hex_color.rb

Defined Under Namespace

Classes: HexError

Instance Attribute Summary collapse

Attributes inherited from ColorType

#alpha, #b, #g, #r

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ColorType

#==, #===, #=~, #alpha?, #average, average, blend, blend_alphas, #build_from_color, byte, byte_to_percentage, color_opts, color_type_class, color_types, #initialize, is_color?, #layer, make, #mix, percent_to_byte, percentage, #ratio, ratio, #render, #render_as_rgba, #to, #to_s, valid_byte?

Constructor Details

This class inherits a constructor from StyleTrain::ColorType

Instance Attribute Details

#hexObject

Returns the value of attribute hex.



9
10
11
# File 'lib/style_train/color_types/hex_color.rb', line 9

def hex
  @hex
end

#hex_6Object

Returns the value of attribute hex_6.



9
10
11
# File 'lib/style_train/color_types/hex_color.rb', line 9

def hex_6
  @hex_6
end

Class Method Details

.expand(hex) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/style_train/color_types/hex_color.rb', line 26

def self.expand( hex ) 
  expanded = if (hex.size == 3)
    str = ''
    (0..2).each do |index|
      char = hex[index..index]
      str << char*2
    end
    str
  else
    hex
  end
  "0x#{expanded}".to_i(16)       
end

Instance Method Details

#buildObject



21
22
23
24
# File 'lib/style_train/color_types/hex_color.rb', line 21

def build 
  self.hex =  "%.2x" % self.r + "%.2x" % self.g + "%.2x" % self.b
  self.hex_6 = ('0x' + self.hex).to_i(16)
end

#render_as_givenObject



40
41
42
# File 'lib/style_train/color_types/hex_color.rb', line 40

def render_as_given
  "##{self.hex}"
end

#type_initialize(color, opts) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
# File 'lib/style_train/color_types/hex_color.rb', line 11

def type_initialize( color, opts ) 
  hex = color.to_s 
  raise HexError unless hex.match(/^#?([a-f0-9]{3})$/i) || hex.match(/^#?([a-f0-9]{6})$/i)
  self.hex = "#{$1}"
  self.hex_6 =  self.class.expand( self.hex ) 
  self.r = (self.hex_6 / 0x10000) & 0xff
  self.g = (self.hex_6 / 0x100) & 0xff
  self.b = (self.hex_6) & 0xff
end