Class: ColorContrastCalc::Color

Inherits:
Object
  • Object
show all
Extended by:
Factory
Includes:
Deprecated::Color
Defined in:
lib/color_contrast_calc/color.rb

Overview

Represent specific colors.

This class also provides lists of predefined colors represented as instances of Color class.

Defined Under Namespace

Modules: Factory, List

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Factory

as_color, color_from, from_hex, from_hsl, from_name, from_rgb

Methods included from Deprecated::Color::Factory

#new_from_hsl

Methods included from Deprecated::Color

#new_brightness_color, #new_contrast_color, #new_grayscale_color, #new_hue_rotate_color, #new_invert_color, #new_saturate_color

Constructor Details

#initialize(rgb, name = nil) ⇒ Color

Create a new instance of Color.

Parameters:

  • rgb (Array<Integer>, String)

    RGB value represented as an array of integers or hex color code such as [255, 255, 0] or “#ffff00”.

  • name (String) (defaults to: nil)

    You can name the color to be created. Without this option, a color keyword name (if exists) or the value of normalized hex color code is assigned instead.



226
227
228
229
230
231
232
# File 'lib/color_contrast_calc/color.rb', line 226

def initialize(rgb, name = nil)
  @rgb = rgb.is_a?(String) ? Utils.hex_to_rgb(rgb) : rgb.dup
  @opacity = @rgb.length == 4 ? @rgb.pop : 1.0
  @hex = Utils.rgb_to_hex(@rgb)
  @name = name || common_name
  @relative_luminance = Checker.relative_luminance(@rgb)
end

Instance Attribute Details

#hexString (readonly)

Returns Hex color code of the color.

Returns:

  • (String)

    Hex color code of the color



214
# File 'lib/color_contrast_calc/color.rb', line 214

attr_reader :rgb, :hex, :name, :relative_luminance, :opacity

#nameString (readonly)

Returns Name of the color.

Returns:

  • (String)

    Name of the color



214
# File 'lib/color_contrast_calc/color.rb', line 214

attr_reader :rgb, :hex, :name, :relative_luminance, :opacity

#opacityObject (readonly)

Returns the value of attribute opacity.



214
215
216
# File 'lib/color_contrast_calc/color.rb', line 214

def opacity
  @opacity
end

#relative_luminanceFloat (readonly)

Returns Relative luminance of the color.

Returns:

  • (Float)

    Relative luminance of the color



214
# File 'lib/color_contrast_calc/color.rb', line 214

attr_reader :rgb, :hex, :name, :relative_luminance, :opacity

#rgbArray<Integer> (readonly)

Returns RGB value of the color.

Returns:

  • (Array<Integer>)

    RGB value of the color



214
215
216
# File 'lib/color_contrast_calc/color.rb', line 214

def rgb
  @rgb
end

Instance Method Details

#common_nameString

Return a color keyword name when the name corresponds to the hex code of the color. Otherwise the hex code will be returned.

Returns:

  • (String)

    Color keyword name or hex color code



275
276
277
# File 'lib/color_contrast_calc/color.rb', line 275

def common_name
  List::HEX_TO_COLOR[@hex]&.name || @hex
end

#complementary(name = nil) ⇒ Color

Return a complementary color of the original color.

Parameters:

  • name (String) (defaults to: nil)

    You can name the color to be created. Without this option, the value of normalized hex color code is assigned instead.

Returns:

  • (Color)

    New complementary color



364
365
366
367
# File 'lib/color_contrast_calc/color.rb', line 364

def complementary(name = nil)
  minmax = rgb.minmax.reduce {|min, max| min + max }
  create(rgb.map {|c| minmax - c }, name)
end

#contrast_level(other_color) ⇒ String

Return the level of contrast ratio defined by WCAG 2.0.

Parameters:

  • other_color (Color, Array<Integer>, String)

    Another instance of Color, RGB value or hex color code

Returns:

  • (String)

    “A”, “AA” or “AAA” if the contrast ratio meets the criteria of WCAG 2.0, otherwise “-”



427
428
429
# File 'lib/color_contrast_calc/color.rb', line 427

def contrast_level(other_color)
  Checker.ratio_to_level(contrast_ratio_against(other_color))
end

#contrast_ratio_against(other_color) ⇒ Float

Calculate the contrast ratio against another color.

Parameters:

  • other_color (Color, Array<Integer>, String)

    Another instance of Color, RGB value or hex color code

Returns:

  • (Float)

    Contrast ratio



410
411
412
413
414
415
416
417
# File 'lib/color_contrast_calc/color.rb', line 410

def contrast_ratio_against(other_color)
  unless other_color.is_a? Color
    return Checker.contrast_ratio(rgb, other_color)
  end

  Checker.luminance_to_contrast_ratio(relative_luminance,
                                      other_color.relative_luminance)
end

#find_brightness_threshold(other_color, level = Checker::Level::AA) ⇒ Color

Try to find a color who has a satisfying contrast ratio.

The returned color is gained by modifying the brightness of another color. Even when a color that satisfies the specified level is not found, it returns a new color anyway.

Parameters:

  • other_color (Color, Array<Integer>, String)

    Color before the adjustment of brightness

  • level (String) (defaults to: Checker::Level::AA)

    “A”, “AA” or “AAA”

Returns:

  • (Color)

    New color whose brightness is adjusted from that of other_color



381
382
383
384
# File 'lib/color_contrast_calc/color.rb', line 381

def find_brightness_threshold(other_color, level = Checker::Level::AA)
  other_color = create(other_color) unless other_color.is_a? Color
  create(ThresholdFinder::Brightness.find(rgb, other_color.rgb, level))
end

#find_lightness_threshold(other_color, level = Checker::Level::AA) ⇒ Color

Try to find a color who has a satisfying contrast ratio.

The returned color is gained by modifying the lightness of another color. Even when a color that satisfies the specified level is not found, it returns a new color anyway.

Parameters:

  • other_color (Color, Array<Integer>, String)

    Color before the adjustment of lightness

  • level (String) (defaults to: Checker::Level::AA)

    “A”, “AA” or “AAA”

Returns:

  • (Color)

    New color whose brightness is adjusted from that of other_color



398
399
400
401
# File 'lib/color_contrast_calc/color.rb', line 398

def find_lightness_threshold(other_color, level = Checker::Level::AA)
  other_color = create(other_color) unless other_color.is_a? Color
  create(ThresholdFinder::Lightness.find(rgb, other_color.rgb, level))
end

#higher_luminance_than?(other_color) ⇒ Boolean

Check if the color has higher luminance than another color.

Parameters:

  • other_color (Color)

    Another color

Returns:

  • (Boolean)

    true if the relative luminance of self is higher than that of other_color



514
515
516
# File 'lib/color_contrast_calc/color.rb', line 514

def higher_luminance_than?(other_color)
  relative_luminance > other_color.relative_luminance
end

#hslArray<Float>

Return HSL value of the color.

The value is calculated from the RGB value, so if you create the instance by Color.from_hsl method, the value used to create the color does not necessarily correspond to the value of this property.

Returns:

  • (Array<Float>)

    HSL value represented as an array of numbers



250
251
252
# File 'lib/color_contrast_calc/color.rb', line 250

def hsl
  @hsl ||= Utils.rgb_to_hsl(@rgb)
end

#hwbArray<Float>

Return HWB value of the color.

The value is calculated from the RGB value, so if you create the instance by Color.color_from method, the value used to create the color does not necessarily correspond to the value of this property.

Returns:

  • (Array<Float>)

    HWB value represented as an array of numbers



264
265
266
# File 'lib/color_contrast_calc/color.rb', line 264

def hwb
  @hwb ||= Utils.rgb_to_hwb(@rgb)
end

#light_color?Boolean

Check if the contrast ratio against black is higher than against white.

Returns:

  • (Boolean)

    true if the contrast ratio against white is qual to or less than the ratio against black



535
536
537
# File 'lib/color_contrast_calc/color.rb', line 535

def light_color?
  Checker.light_color?(rgb)
end

#max_contrast?Boolean

Check if the color reachs already the max contrast.

The max contrast in this context means that of colors modified by the operation defined at

Returns:

  • (Boolean)

    true if self.with_contrast® where r is greater than 100 returns the same color as self.



491
492
493
# File 'lib/color_contrast_calc/color.rb', line 491

def max_contrast?
  rgb.all? {|c| RGB_LIMITS.include? c }
end

#min_contrast?Boolean

Check if the color reachs already the min contrast.

The min contrast in this context means that of colors modified by the operation defined at

Returns:

  • (Boolean)

    true if self is the same color as “#808080”



503
504
505
# File 'lib/color_contrast_calc/color.rb', line 503

def min_contrast?
  rgb == GRAY.rgb
end

#same_color?(other_color) ⇒ Boolean

Check it two colors have the same RGB value.

Parameters:

  • other_color (Color, Array<Integer>, String)

    Another instance of Color, RGB value or hex color code

Returns:

  • (Boolean)

    true if other_color has the same RGB value



471
472
473
474
475
476
477
478
479
480
# File 'lib/color_contrast_calc/color.rb', line 471

def same_color?(other_color)
  case other_color
  when Color
    hex == other_color.hex
  when Array
    hex == Utils.rgb_to_hex(other_color)
  when String
    hex == Utils.normalize_hex(other_color)
  end
end

#same_luminance_as?(other_color) ⇒ Boolean

Check if two colors has the same relative luminance.

Parameters:

  • other_color (Color)

    Another color

Returns:

  • (Boolean)

    true if the relative luminance of self and other_color are same.



525
526
527
# File 'lib/color_contrast_calc/color.rb', line 525

def same_luminance_as?(other_color)
  relative_luminance == other_color.relative_luminance
end

#sufficient_contrast?(other_color, level = Checker::Level::AA) ⇒ Boolean

Check if the contrast ratio with another color meets a WCAG 2.0 criterion.

Parameters:

  • other_color (Color, Array<Integer>, String)

    Another instance of Color, RGB value or hex color code

  • level (String) (defaults to: Checker::Level::AA)

    “A”, “AA” or “AAA”

Returns:

  • (Boolean)

    true if the contrast ratio meets the specified level



459
460
461
462
# File 'lib/color_contrast_calc/color.rb', line 459

def sufficient_contrast?(other_color, level = Checker::Level::AA)
  ratio = Checker.level_to_ratio(level)
  contrast_ratio_against(other_color) >= ratio
end

#to_s(base = 16) ⇒ String

Return a string representation of the color.

Parameters:

  • base (Ingeger, nil) (defaults to: 16)

    16, 10 or nil. when base = 16, a hex color code such as “#ffff00” is returned, and when base = 10, a code in RGB notation such as “rgb(255, 255, 0)”

Returns:

  • (String)

    String representation of the color



439
440
441
442
443
444
445
446
447
448
# File 'lib/color_contrast_calc/color.rb', line 439

def to_s(base = 16)
  case base
  when 16
    hex
  when 10
    @rgb_code ||= format('rgb(%d,%d,%d)', *rgb)
  else
    name
  end
end

#with_brightness(ratio, name = nil) ⇒ Color

Return a new instance of Color with adjusted brightness.

Parameters:

  • ratio (Float)

    Adjustment ratio in percentage

  • name (String) (defaults to: nil)

    You can name the color to be created. Without this option, the value of normalized hex color code is assigned instead.

Returns:

  • (Color)

    New color with adjusted brightness



301
302
303
# File 'lib/color_contrast_calc/color.rb', line 301

def with_brightness(ratio, name = nil)
  generate_new_color(Converter::Brightness, ratio, name)
end

#with_contrast(ratio, name = nil) ⇒ Color

Return a new instance of Color with adjusted contrast.

Parameters:

  • ratio (Float)

    Adjustment ratio in percentage

  • name (String) (defaults to: nil)

    You can name the color to be created. Without this option, the value of normalized hex color code is assigned instead.

Returns:

  • (Color)

    New color with adjusted contrast



288
289
290
# File 'lib/color_contrast_calc/color.rb', line 288

def with_contrast(ratio, name = nil)
  generate_new_color(Converter::Contrast, ratio, name)
end

#with_grayscale(ratio = 100, name = nil) ⇒ Color

Return a grayscale of the original color.

Parameters:

  • ratio (Float) (defaults to: 100)

    Conversion ratio in percentage

  • name (String) (defaults to: nil)

    You can name the color to be created. Without this option, the value of normalized hex color code is assigned instead.

Returns:

  • (Color)

    New grayscale color



353
354
355
# File 'lib/color_contrast_calc/color.rb', line 353

def with_grayscale(ratio = 100, name = nil)
  generate_new_color(Converter::Grayscale, ratio, name)
end

#with_hue_rotate(degree, name = nil) ⇒ Color

Return a hue rotation applied color as an instance of Color.

Parameters:

  • degree (Float)

    Degrees of rotation (0 to 360)

  • name (String) (defaults to: nil)

    You can name the color to be created. Without this option, the value of normalized hex color code is assigned instead.

Returns:

  • (Color)

    New hue rotation applied color



327
328
329
# File 'lib/color_contrast_calc/color.rb', line 327

def with_hue_rotate(degree, name = nil)
  generate_new_color(Converter::HueRotate, degree, name)
end

#with_invert(ratio = 100, name = nil) ⇒ Color

Return an inverted color as an instance of Color.

Parameters:

  • ratio (Float) (defaults to: 100)

    Proportion of the conversion in percentage

  • name (String) (defaults to: nil)

    You can name the color to be created. Without this option, the value of normalized hex color code is assigned instead.

Returns:

  • (Color)

    New inverted color



314
315
316
# File 'lib/color_contrast_calc/color.rb', line 314

def with_invert(ratio = 100, name = nil)
  generate_new_color(Converter::Invert, ratio, name)
end

#with_saturate(ratio, name = nil) ⇒ Color

Return a saturated color as an instance of Color.

Parameters:

  • ratio (Float)

    Proprtion of the conversion in percentage

  • name (String) (defaults to: nil)

    You can name the color to be created. Without this option, the value of normalized hex color code is assigned instead.

Returns:

  • (Color)

    New saturated color



340
341
342
# File 'lib/color_contrast_calc/color.rb', line 340

def with_saturate(ratio, name = nil)
  generate_new_color(Converter::Saturate, ratio, name)
end