Class: Magick::Pixel

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

Instance Method Summary collapse

Instance Method Details

#is_black_or_white?Boolean



23
24
25
26
27
28
# File 'lib/paletti/pixel.rb', line 23

def is_black_or_white?
  r, g, b = self.norm_red, self.norm_green, self.norm_blue
  black_thresh = 0.09 * 255.to_f
  white_thresh = 0.91 * 255.to_f
  (r > white_thresh && g > white_thresh && b > white_thresh) || (r < black_thresh && g < black_thresh && b < black_thresh) ? true : false
end

#is_contrasting?(other_pixel) ⇒ Boolean



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/paletti/pixel.rb', line 45

def is_contrasting?(other_pixel)
  # Calculate contrast using the W3C formula
  #   http://www.w3.org/TR/WCAG20-TECHS/G145.html
  lum = self.luminance
  other_pixel_lum = other_pixel.luminance
  contrast = 0
  if lum > other_pixel_lum
    contrast = (lum + 0.05) / (other_pixel_lum + 0.05)
  else
    contrast = (other_pixel_lum + 0.05) / (lum + 0.05)
  end

  return contrast.to_f > 3
end

#is_dark?Boolean



60
61
62
# File 'lib/paletti/pixel.rb', line 60

def is_dark?
  return self.luminance < 0.5
end

#is_distinct?(other_pixel) ⇒ Boolean



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/paletti/pixel.rb', line 30

def is_distinct?(other_pixel)
  r, g, b, a = self.norm_red, self.norm_green, self.norm_blue, self.norm_opacity
  other_r, other_g, other_b, other_a = other_pixel.norm_red, other_pixel.norm_green, other_pixel.norm_blue, other_pixel.norm_opacity
  upper_thresh = 0.25 * 255.to_f
  lower_thresh = 0.03 * 255.to_f

  if (r - other_r).abs > upper_thresh || (g - other_g).abs > upper_thresh || (b - other_b).abs > upper_thresh || (a - other_a).abs > upper_thresh
    if (r - g).abs < lower_thresh && (r - b).abs < lower_thresh && (other_r - other_g).abs < lower_thresh && (other_r - other_b).abs < lower_thresh
      return false
    end
    return true
  end
  return false
end

#luminanceObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/paletti/pixel.rb', line 64

def luminance
  r, g, b = self.norm_red / 255.to_f, self.norm_green / 255.to_f, self.norm_blue / 255.to_f
  if r <= 0.03928
    r = r / 12.92
  else
    r = ((r + 0.055) / 1.055) ** 2.4
  end

  if g <= 0.03928
    g = g / 12.92
  else
    g = ((g + 0.055) / 1.055) ** 2.4
  end

  if b <= 0.03928
    b = b / 12.92
  else
    b = ((b + 0.055) / 1.055) ** 2.4
  end

  return 0.2126 * r + 0.7152 * g + 0.0722 * b
end

#norm_blueObject



15
16
17
# File 'lib/paletti/pixel.rb', line 15

def norm_blue
  self.blue.to_f * NORM_FACTOR
end

#norm_greenObject



11
12
13
# File 'lib/paletti/pixel.rb', line 11

def norm_green
  self.green.to_f * NORM_FACTOR
end

#norm_opacityObject



19
20
21
# File 'lib/paletti/pixel.rb', line 19

def norm_opacity
  self.opacity.to_f * NORM_FACTOR
end

#norm_redObject



7
8
9
# File 'lib/paletti/pixel.rb', line 7

def norm_red
  self.red.to_f * NORM_FACTOR
end

#to_norm_rgbaObject



3
4
5
# File 'lib/paletti/pixel.rb', line 3

def to_norm_rgba
  [self.red.to_f * NORM_FACTOR, self.green.to_f * NORM_FACTOR, self.blue.to_f * NORM_FACTOR, self.opacity.to_f * NORM_FACTOR]
end