class Photomosaic::Color::RGB

Attributes

blue[R]
green[R]
red[R]

Public Class Methods

new(red, green, blue) click to toggle source
# File lib/photomosaic/color/rgb.rb, line 6
def initialize(red, green, blue)
  @red = red
  @green = green
  @blue = blue
end

Public Instance Methods

calculate_distance(rgb) click to toggle source
# File lib/photomosaic/color/rgb.rb, line 24
def calculate_distance(rgb)
  Math.sqrt(squares_array(rgb).inject(&:+))
end
max() click to toggle source
# File lib/photomosaic/color/rgb.rb, line 12
def max
  @max ||= [@red, @green, @blue].max
end
min() click to toggle source
# File lib/photomosaic/color/rgb.rb, line 16
def min
  @min ||= [@red, @green, @blue].min
end
to_hsv() click to toggle source
# File lib/photomosaic/color/rgb.rb, line 20
def to_hsv
  HSV.new(hue, saturation, value)
end

Private Instance Methods

hue() click to toggle source
# File lib/photomosaic/color/rgb.rb, line 30
def hue
  return 0 if max == min

  _hue = case max
         when @red
           ((@green - @blue).to_f / (max - min)) % 6
         when @green
           (@blue - @red).to_f / (max - min) + 2
         else
           (@red - @green).to_f / (max - min) + 4
         end

  (_hue * 60).to_i
end
saturation() click to toggle source
# File lib/photomosaic/color/rgb.rb, line 45
def saturation
  max == 0 ? 0 : (max - min).to_f / max * 100
end
squares_array(rgb) click to toggle source
# File lib/photomosaic/color/rgb.rb, line 49
def squares_array(rgb)
  [
   (self.red - rgb.red)**2,
   (self.green - rgb.green)**2,
   (self.blue - rgb.blue)**2
  ]
end
value() click to toggle source
# File lib/photomosaic/color/rgb.rb, line 57
def value
  (max * 100).to_f / 256
end