Class Color::Palette::MonoContrast
In: lib/color/palette/monocontrast.rb
Parent: Object
Enumerable Gimp AdobeColor CMYK\n[lib/color.rb\nlib/color/cmyk.rb] GrayScale\n[lib/color.rb\nlib/color/grayscale.rb] RGB\n[lib/color.rb\nlib/color/rgb-colors.rb\nlib/color/rgb.rb] YIQ\n[lib/color.rb\nlib/color/yiq.rb] HSL MonoContrast lib/color/cmyk.rb lib/color/grayscale.rb lib/color/rgb.rb lib/color/yiq.rb lib/color/hsl.rb CSS lib/color/palette/gimp.rb lib/color/palette/adobecolor.rb lib/color/palette/monocontrast.rb Palette Color dot/m_10_0.png

Generates a monochromatic constrasting colour palette for background and foreground. What does this mean?

Monochromatic: A single colour is used to generate the base palette, and this colour is lightened five times and darkened five times to provide eleven distinct colours.

Contrasting: The foreground is also generated as a monochromatic colour palette; however, all generated colours are tested to see that they are appropriately contrasting to ensure maximum readability of the foreground against the background.

Methods

Constants

DEFAULT_MINIMUM_BRIGHTNESS_DIFF = (125.0 / 255.0)
DEFAULT_MINIMUM_COLOR_DIFF = (500.0 / 255.0)

Attributes

background  [R]  Hash of CSS background colour values.

This is always 11 values:

0:The starting colour.
+1..+5:Lighter colours.
-1..-5:Darker colours.
foreground  [R]  Hash of CSS foreground colour values.

This is always 11 values:

0:The starting colour.
+1..+5:Lighter colours.
-1..-5:Darker colours.
minimum_brightness_diff  [RW]  The minimum brightness difference between the background and the foreground, and must be between 0..1. Setting this value will regenerate the palette based on the base colours. The default value for this is 125 / 255.0. If this value is set to nil, it will be restored to the default.
minimum_color_diff  [RW]  The minimum colour difference between the background and the foreground, and must be between 0..3. Setting this value will regenerate the palette based on the base colours. The default value for this is 500 / 255.0.

Public Class methods

Generate the initial palette.

[Source]

    # File lib/color/palette/monocontrast.rb, line 90
90:   def initialize(background, foreground = nil)
91:     @minimum_brightness_diff = DEFAULT_MINIMUM_BRIGHTNESS_DIFF
92:     @minimum_color_diff = DEFAULT_MINIMUM_COLOR_DIFF
93: 
94:     regenerate(background, foreground)
95:   end

Public Instance methods

Returns the absolute difference between the brightness levels of two colours. This will be a decimal value between 0 and 1. W3C accessibility guidelines for colour contrast suggest that this value be at least approximately 0.49 (125 / 255.0) for proper contrast.

[Source]

     # File lib/color/palette/monocontrast.rb, line 168
168:   def brightness_diff(c1, c2)
169:     (c1.brightness - c2.brightness).abs
170:   end

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.

[Source]

     # File lib/color/palette/monocontrast.rb, line 136
136:   def calculate_foreground(background, foreground)
137:     nfg = nil
138:     # Loop through brighter and darker versions of the foreground color. The
139:     # numbers here represent the amount of foreground color to mix with
140:     # black and white.
141:     [100, 75, 50, 25, 0].each do |percent|
142:       dfg = foreground.darken_by(percent)
143:       lfg = foreground.lighten_by(percent)
144: 
145:       dbd = brightness_diff(background, dfg)
146:       lbd = brightness_diff(background, lfg)
147: 
148:       if lbd > dbd
149:         nfg = lfg
150:         nbd = lbd
151:       else
152:         nfg = dfg
153:         nbd = dbd
154:       end
155: 
156:       ncd = color_diff(background, nfg)
157: 
158:       break if nbd >= @minimum_brightness_diff and ncd >= @minimum_color_diff
159:     end
160:     nfg
161:   end

Returns the contrast between to colours, a decimal value between 0 and

  1. W3C accessibility guidelines for colour

contrast suggest that this value be at least approximately 1.96 (500 / 255.0) for proper contrast.

[Source]

     # File lib/color/palette/monocontrast.rb, line 176
176:   def color_diff(c1, c2)
177:     r = (c1.r - c2.r).abs
178:     g = (c1.g - c2.g).abs
179:     b = (c1.b - c2.b).abs
180:     r + g + b
181:   end

[Source]

    # File lib/color/palette/monocontrast.rb, line 76
76:   def minimum_color_diff=(cd) #:noco:
77:     if cd.nil?
78:       @minimum_color_diff = DEFAULT_MINIMUM_COLOR_DIFF
79:     elsif cd > 3.0
80:       @minimum_color_diff = 3.0
81:     elsif cd < 0.0
82:       @minimum_color_diff = 0.0
83:     else
84:       @minimum_color_diff = cd
85:     end
86:     regenerate(@background[0], @foreground[0])
87:   end

Generate the colour palettes.

[Source]

     # File lib/color/palette/monocontrast.rb, line 98
 98:   def regenerate(background, foreground = nil)
 99:     foreground ||= background
100:     background = background.to_rgb
101:     foreground = foreground.to_rgb
102: 
103:     @background = {}
104:     @foreground = {}
105: 
106:     @background[-5] = background.darken_by(10)
107:     @background[-4] = background.darken_by(25)
108:     @background[-3] = background.darken_by(50)
109:     @background[-2] = background.darken_by(75)
110:     @background[-1] = background.darken_by(85)
111:     @background[ 0] = background
112:     @background[+1] = background.lighten_by(85)
113:     @background[+2] = background.lighten_by(75)
114:     @background[+3] = background.lighten_by(50)
115:     @background[+4] = background.lighten_by(25)
116:     @background[+5] = background.lighten_by(10)
117: 
118:     @foreground[-5] = calculate_foreground(@background[-5], foreground)
119:     @foreground[-4] = calculate_foreground(@background[-4], foreground)
120:     @foreground[-3] = calculate_foreground(@background[-3], foreground)
121:     @foreground[-2] = calculate_foreground(@background[-2], foreground)
122:     @foreground[-1] = calculate_foreground(@background[-1], foreground)
123:     @foreground[ 0] = calculate_foreground(@background[ 0], foreground)
124:     @foreground[+1] = calculate_foreground(@background[+1], foreground)
125:     @foreground[+2] = calculate_foreground(@background[+2], foreground)
126:     @foreground[+3] = calculate_foreground(@background[+3], foreground)
127:     @foreground[+4] = calculate_foreground(@background[+4], foreground)
128:     @foreground[+5] = calculate_foreground(@background[+5], foreground)
129:   end

[Validate]