Class: Pixelfy

Inherits:
Object
  • Object
show all
Includes:
Chroma
Defined in:
lib/stitchify/pixelfy.rb

Constant Summary collapse

HSL_OPEN_CONST =
"hsl("

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hue, saturation, lightness, shape = nil) ⇒ Pixelfy

Returns a new instance of Pixelfy.



9
10
11
12
13
14
# File 'lib/stitchify/pixelfy.rb', line 9

def initialize(hue, saturation, lightness, shape=nil)
  self.hue = hue
  self.saturation = saturation
  self.lightness = lightness
  self.shape = shape
end

Instance Attribute Details

#hueObject

Returns the value of attribute hue.



5
6
7
# File 'lib/stitchify/pixelfy.rb', line 5

def hue
  @hue
end

#lightnessObject

Returns the value of attribute lightness.



5
6
7
# File 'lib/stitchify/pixelfy.rb', line 5

def lightness
  @lightness
end

#saturationObject

Returns the value of attribute saturation.



5
6
7
# File 'lib/stitchify/pixelfy.rb', line 5

def saturation
  @saturation
end

#shapeObject

Returns the value of attribute shape.



5
6
7
# File 'lib/stitchify/pixelfy.rb', line 5

def shape
  @shape
end

Class Method Details

.from_hex(hex_str) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stitchify/pixelfy.rb', line 16

def self.from_hex(hex_str)
  px = ''
  if hex_str
    color_str = Chroma::Color.new(hex_str).to_hsl
    color_str.slice!(HSL_OPEN_CONST)
    color_str.slice!("%)")
    color_str.slice!('%')
    color_arr = color_str.split(', ')

    px = self.new(color_arr[0].to_i, color_arr[1].to_i, color_arr[2].to_i)
  end
  px
end

Instance Method Details

#colorize(map) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/stitchify/pixelfy.rb', line 49

def colorize(map)
  deltae = 100000000000
  closest_px = map[0]

  map.each do |dom_px|
    hue_delta = (dom_px.hue - self.hue).abs

    if hue_delta < deltae

      deltae = hue_delta
      closest_px = dom_px

    elsif hue_delta == deltae
      dom_px_delta = self.get_full_delta(dom_px)
      current_px_delta = self.get_full_delta(closest_px)
      if dom_px_delta < current_px_delta
          deltae = hue_delta
          closest_px = dom_px
      end
    end 
  end

  self.hue = closest_px.hue
  self.saturation = closest_px.saturation
  self.lightness = closest_px.lightness
  self.shape = closest_px.shape

  self
end

#get_full_delta(px) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/stitchify/pixelfy.rb', line 79

def get_full_delta(px)
  hue_delta = (px.hue - self.hue).abs
  sat_delta = (px.saturation - self.saturation).abs
  lit_delta = (px.lightness - self.lightness).abs

  hue_delta + sat_delta + lit_delta
end

#hexObject



30
31
32
# File 'lib/stitchify/pixelfy.rb', line 30

def hex
  Chroma::Color.new(hsl).to_hex
end

#hslObject



34
35
36
# File 'lib/stitchify/pixelfy.rb', line 34

def hsl
  "#{HSL_OPEN_CONST} #{self.hue}, #{self.saturation}%, #{self.lightness}%)"
end

#hslaObject



38
39
40
# File 'lib/stitchify/pixelfy.rb', line 38

def hsla
  "#{HSL_OPEN_CONST} #{self.hue}, #{self.saturation}%, #{self.lightness}%, 1)"
end

#nameObject



42
43
44
45
46
47
# File 'lib/stitchify/pixelfy.rb', line 42

def name
  n = ''
  c = Chroma::Color.new(hsl).to_name
  n = c unless c == "<unknown>"

end