class ColorUtil::Color

Overview

Specifies a 24 bit color.

Included Modules

Defined in:

colorutil/color.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.from_hex(color : Int32 | UInt32) : Color #

Creates a color from an rgb hexidecimal value. For example, Color.from_hex(0xff00ff) creates the fuchsia usually described as "#ff00ff".


[View source]
def self.from_hex(color : String) : Color #

[View source]
def self.from_hsl(h : Float64, s : Float64, l : Float64) : Color #

Creates a Color using HSLuv hsl components.


[View source]
def self.from_rgb(r : Number, g : Number, b : Number) : Color #

Creates a Color from individual rgb values.


[View source]
def self.from_spectrum(spectrum : Indexable) : Color #

[View source]
def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) #

[View source]
def self.random : Color #

Generates a random Color.


[View source]

Class Method Detail

def self.mix(start : Color, stop : Color, frac) #

TODO Refactor to use the interpolation function Interpolates between two colors. When frac == 0, returns start. As frac approaches 1, the function returns a color closer and closer to stop.


[View source]

Instance Method Detail

def ==(other : Color) : Bool #

True if and only if each rgb color component is equal among both colors.


[View source]
def approx_contrast(other : Color) : Float64 #

Returns the luminance contrast ratio between this color and another. Computed using approximate relative luminance. Overall, this function has an error of at most 20% on the exact contrast, but note that this error is much smaller when colors are similar. So, for determining if two colors are readable together, this is likely accurate enough.


[View source]
def approx_relative_luminance : Float64 #

Computes the approximate relative luminance of this color. See ColorUtil#approx_relative_luminance


[View source]
def b : UInt8 #

[View source]
def b=(b) #

[View source]
def contrast(other : Color) : Float64 #

Returns the luminance contrast ratio between this color and another. Computed using WCAG relative luminance.


[View source]
def g : UInt8 #

[View source]
def g=(g) #

[View source]
def generate(contrast : Float64, hue : Float64, saturation : Float64, min_lightness : Float64 = 0, max_lightness : Float64 = 100) : Color #

Generates a new color that has a specified contrast ratio to self. As contrast ratio is computed only using lightness, this leaves hue and saturation as free parameters which the user can choose.


[View source]
def h : Float64 #

[View source]
def l : Float64 #

[View source]
def r : UInt8 #

[View source]
def r=(r) #

[View source]
def relative_luminance : Float64 #

Returns the relative luminance of this color, as defined by https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests. This is needed to compute contrast acording to the web content accessibility guidelines.


[View source]
def rgb : NamedTuple(r: UInt8, g: UInt8, b: UInt8) #

[View source]
def s : Float64 #

[View source]
def to_a : Array(Float64) #

Returns an [h, s, l] array storing color data.


[View source]
def to_hex : UInt32 #

Returns the raw 24-bit integer value that represents this color. For example, pure blue (0x0000ff) would return the integer 255. This is effectively the inverse to the constructor "from_hex".


[View source]
def to_hex_string : String #

Returns a human readable hex color code - e.g. "#ff00ff" for pure fuchsia.


[View source]
def to_s(io : IO) : Nil #

By default, the string version of a color is it's hex string.


[View source]