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.
-
#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.
229 230 231 232 233 234 235 |
# File 'lib/color_contrast_calc/color.rb', line 229 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.
217 |
# File 'lib/color_contrast_calc/color.rb', line 217 attr_reader :rgb, :hex, :name, :relative_luminance, :opacity |
#name ⇒ String (readonly)
Returns Name of the color.
217 |
# File 'lib/color_contrast_calc/color.rb', line 217 attr_reader :rgb, :hex, :name, :relative_luminance, :opacity |
#opacity ⇒ Object (readonly)
Returns the value of attribute opacity.
217 218 219 |
# File 'lib/color_contrast_calc/color.rb', line 217 def opacity @opacity end |
#relative_luminance ⇒ Float (readonly)
Returns Relative luminance of the color.
217 |
# File 'lib/color_contrast_calc/color.rb', line 217 attr_reader :rgb, :hex, :name, :relative_luminance, :opacity |
#rgb ⇒ Array<Integer> (readonly)
Returns RGB value of the color.
217 218 219 |
# File 'lib/color_contrast_calc/color.rb', line 217 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.
258 259 260 261 |
# File 'lib/color_contrast_calc/color.rb', line 258 def common_name named_color = List::HEX_TO_COLOR[@hex] named_color && named_color.name || @hex end |
#complementary(name = nil) ⇒ Color
Return a complementary color of the original color.
348 349 350 351 |
# File 'lib/color_contrast_calc/color.rb', line 348 def complementary(name = nil) minmax = rgb.minmax.reduce {|min, max| min + max } self.class.new(rgb.map {|c| minmax - c }, name) end |
#contrast_level(other_color) ⇒ String
Return the level of contrast ratio defined by WCAG 2.0.
411 412 413 |
# File 'lib/color_contrast_calc/color.rb', line 411 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.
394 395 396 397 398 399 400 401 |
# File 'lib/color_contrast_calc/color.rb', line 394 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.
365 366 367 368 |
# File 'lib/color_contrast_calc/color.rb', line 365 def find_brightness_threshold(other_color, level = Checker::Level::AA) other_color = Color.new(other_color) unless other_color.is_a? Color Color.new(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.
382 383 384 385 |
# File 'lib/color_contrast_calc/color.rb', line 382 def find_lightness_threshold(other_color, level = Checker::Level::AA) other_color = Color.new(other_color) unless other_color.is_a? Color Color.new(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.
498 499 500 |
# File 'lib/color_contrast_calc/color.rb', line 498 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.
247 248 249 |
# File 'lib/color_contrast_calc/color.rb', line 247 def hsl @hsl ||= Utils.rgb_to_hsl(@rgb) end |
#light_color? ⇒ Boolean
Check if the contrast ratio against black is higher than against white.
519 520 521 |
# File 'lib/color_contrast_calc/color.rb', line 519 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
475 476 477 |
# File 'lib/color_contrast_calc/color.rb', line 475 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
487 488 489 |
# File 'lib/color_contrast_calc/color.rb', line 487 def min_contrast? rgb == GRAY.rgb end |
#same_color?(other_color) ⇒ Boolean
Check it two colors have the same RGB value.
455 456 457 458 459 460 461 462 463 464 |
# File 'lib/color_contrast_calc/color.rb', line 455 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.
509 510 511 |
# File 'lib/color_contrast_calc/color.rb', line 509 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.
443 444 445 446 |
# File 'lib/color_contrast_calc/color.rb', line 443 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.
423 424 425 426 427 428 429 430 431 432 |
# File 'lib/color_contrast_calc/color.rb', line 423 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.
285 286 287 |
# File 'lib/color_contrast_calc/color.rb', line 285 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.
272 273 274 |
# File 'lib/color_contrast_calc/color.rb', line 272 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.
337 338 339 |
# File 'lib/color_contrast_calc/color.rb', line 337 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.
311 312 313 |
# File 'lib/color_contrast_calc/color.rb', line 311 def with_hue_rotate(degree, name = nil) generate_new_color(Converter::HueRotate, degree, name) end |