Class: Kracker::Zooka::Pixel

Inherits:
Object
  • Object
show all
Defined in:
lib/kracker/zooka/pixel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color_value) ⇒ Pixel

Returns a new instance of Pixel.



6
7
8
9
10
11
# File 'lib/kracker/zooka/pixel.rb', line 6

def initialize(color_value)
  @alpha = ChunkyPNG::Color.a(color_value)
  @red   = ChunkyPNG::Color.r(color_value)
  @green = ChunkyPNG::Color.g(color_value)
  @blue  = ChunkyPNG::Color.b(color_value)
end

Instance Attribute Details

#alphaObject

Returns the value of attribute alpha.



4
5
6
# File 'lib/kracker/zooka/pixel.rb', line 4

def alpha
  @alpha
end

#blueObject

Returns the value of attribute blue.



4
5
6
# File 'lib/kracker/zooka/pixel.rb', line 4

def blue
  @blue
end

#greenObject

Returns the value of attribute green.



4
5
6
# File 'lib/kracker/zooka/pixel.rb', line 4

def green
  @green
end

#redObject

Returns the value of attribute red.



4
5
6
# File 'lib/kracker/zooka/pixel.rb', line 4

def red
  @red
end

Instance Method Details

#brightnessObject



13
14
15
# File 'lib/kracker/zooka/pixel.rb', line 13

def brightness
  0.3 * @red + 0.59 * @green + 0.11 * @blue
end

#hueObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kracker/zooka/pixel.rb', line 17

def hue
  red   = @red   / 255.0
  green = @green / 255.0
  blue  = @blue  / 255.0

  min = [red, green , blue].sort.first
  max = [red, green , blue].sort.last

  h = 0
  d = 0

  if max == min
    h = 0
  else
    d = max - min
    h = case max
    when red
      (green  - blue) / d + (green  < blue ? 6 : 0)
    when green
      (blue - red) / d + 2
    when blue
      (red - green ) / d + 4
    else
      0
    end

    h = h / 6.0
  end
  h
end