Method: Color::Palette::MonoContrast#calculate_foreground

Defined in:
lib/color/palette/monocontrast.rb

#calculate_foreground(background, foreground) ⇒ Object

Given a background colour and a foreground colour, modifies the foreground colour so that it will have enough contrast to be seen against the background colour.

Uses #mininum_brightness_diff and #minimum_color_diff.


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/color/palette/monocontrast.rb', line 118

def calculate_foreground(background, foreground)
  nfg = nil
  # Loop through brighter and darker versions of the foreground color. The
  # numbers here represent the amount of foreground color to mix with
  # black and white.
  [100, 75, 50, 25, 0].each do |percent|
    dfg = foreground.darken_by(percent)
    lfg = foreground.lighten_by(percent)

    dbd = brightness_diff(background, dfg)
    lbd = brightness_diff(background, lfg)

    if lbd > dbd
      nfg = lfg
      nbd = lbd
    else
      nfg = dfg
      nbd = dbd
    end

    ncd = color_diff(background, nfg)

    break if nbd >= @minimum_brightness_diff and ncd >= @minimum_color_diff
  end
  nfg
end