Class: ColorContrastCalc::Color
- Inherits:
-
Object
- Object
- ColorContrastCalc::Color
- 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
Instance Attribute Summary collapse
-
#hex ⇒ String
readonly
Hex color code of the color.
-
#name ⇒ String
readonly
Name of the color.
-
#opacity ⇒ Object
readonly
Returns the value of attribute opacity.
-
#relative_luminance ⇒ Float
readonly
Relative luminance of the color.
-
#rgb ⇒ Array<Integer>
readonly
RGB value of the color.
Instance Method Summary collapse
-
#common_name ⇒ String
Return a color keyword name when the name corresponds to the hex code of the color.
-
#complementary(name = nil) ⇒ Color
Return a complementary color of the original color.
-
#contrast_level(other_color) ⇒ String
Return the level of contrast ratio defined by WCAG 2.0.
-
#contrast_ratio_against(other_color) ⇒ Float
Calculate the contrast ratio against another color.
-
#find_brightness_threshold(other_color, level = Checker::Level::AA) ⇒ Color
Try to find a color who has a satisfying contrast ratio.
-
#find_lightness_threshold(other_color, level = Checker::Level::AA) ⇒ Color
Try to find a color who has a satisfying contrast ratio.
-
#higher_luminance_than?(other_color) ⇒ Boolean
Check if the color has higher luminance than another color.
-
#hsl ⇒ Array<Float>
Return HSL value of the color.
-
#hwb ⇒ Array<Float>
Return HWB value of the color.
-
#initialize(rgb, name = nil) ⇒ Color
constructor
Create a new instance of Color.
-
#light_color? ⇒ Boolean
Check if the contrast ratio against black is higher than against white.
-
#max_contrast? ⇒ Boolean
Check if the color reachs already the max contrast.
-
#min_contrast? ⇒ Boolean
Check if the color reachs already the min contrast.
-
#same_color?(other_color) ⇒ Boolean
Check it two colors have the same RGB value.
-
#same_luminance_as?(other_color) ⇒ Boolean
Check if two colors has the same relative luminance.
-
#sufficient_contrast?(other_color, level = Checker::Level::AA) ⇒ Boolean
Check if the contrast ratio with another color meets a WCAG 2.0 criterion.
-
#to_s(base = 16) ⇒ String
Return a string representation of the color.
-
#with_brightness(ratio, name = nil) ⇒ Color
Return a new instance of Color with adjusted brightness.
-
#with_contrast(ratio, name = nil) ⇒ Color
Return a new instance of Color with adjusted contrast.
-
#with_grayscale(ratio = 100, name = nil) ⇒ Color
Return a grayscale of the original color.
-
#with_hue_rotate(degree, name = nil) ⇒ Color
Return a hue rotation applied color as an instance of Color.
-
#with_invert(ratio = 100, name = nil) ⇒ Color
Return an inverted color as an instance of Color.
-
#with_saturate(ratio, name = nil) ⇒ Color
Return a saturated color as an instance of Color.
Methods included from Factory
as_color, color_from, from_hex, from_hsl, from_name, from_rgb
Methods included from Deprecated::Color::Factory
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.
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
#hex ⇒ String (readonly)
Returns Hex color code of the color.
214 |
# File 'lib/color_contrast_calc/color.rb', line 214 attr_reader :rgb, :hex, :name, :relative_luminance, :opacity |
#name ⇒ String (readonly)
Returns Name of the color.
214 |
# File 'lib/color_contrast_calc/color.rb', line 214 attr_reader :rgb, :hex, :name, :relative_luminance, :opacity |
#opacity ⇒ Object (readonly)
Returns the value of attribute opacity.
214 215 216 |
# File 'lib/color_contrast_calc/color.rb', line 214 def opacity @opacity end |
#relative_luminance ⇒ Float (readonly)
Returns Relative luminance of the color.
214 |
# File 'lib/color_contrast_calc/color.rb', line 214 attr_reader :rgb, :hex, :name, :relative_luminance, :opacity |
#rgb ⇒ Array<Integer> (readonly)
Returns 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_name ⇒ String
Return a color keyword name when the name corresponds to the hex code of the color. Otherwise the hex code will be returned.
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.
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.
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.
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.
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.
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.
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 |
#hsl ⇒ Array<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.
250 251 252 |
# File 'lib/color_contrast_calc/color.rb', line 250 def hsl @hsl ||= Utils.rgb_to_hsl(@rgb) end |
#hwb ⇒ Array<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.
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.
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
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
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.
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.
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.
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.
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.
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.
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.
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.
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 |