color

Utilities for working with color in Koto.

At the core of the library is the Color type, with various initializers available.

For convenience, the color module itself is callable:

|String| -> Color

Equivalent to calling color.named.

|Number, Number, Number| -> Color

Equivalent to calling color.rgb.

|Number, Number, Number, Number| -> Color

Equivalent to calling color.rgba.

Example:

color 'red'
# -> Color {r: 1, g: 0, b: 0, a: 1}

hsl

|Number, Number, Number| -> Color

Returns a color produced from hue, saturation, and lightness components.

The hue component is specified in degrees.

The saturation and lightness components are specified as numbers between 0 and 1.

Example

color.hsl 180, 1, 0.25
# -> Color {r: 0, g: 0.5, b: 0.5, a: 1}

hsv

|Number, Number, Number| -> Color

Returns a color produced from hue, saturation, and value components.

The hue component is specified in degrees.

The saturation and value components are specified as numbers between 0 and 1.

Example

color.hsv 90, 0.5, 1
# -> Color {r: 0.75, g: 1, b: 0.5, a: 1}

named

|String| -> Color

Returns a color corresponding to one of the named colors listed in the SVG color keywords specification.

Example

color.named 'yellow'
# -> Color {r: 1, g: 1, b: 0, a: 1}

rgb

|Number, Number, Number| -> Color

Returns a color produced from red, green, and blue components.

The RGB components are specified as numbers between 0 and 1.

Example

color.rgb 0.5, 0.1, 0.9
# -> Color {r: 0.5, g: 0.1, b: 0.9, a: 1}

rgba

|Number, Number, Number, Number| -> Color

Returns a color produced from red, green, blue, and alpha components.

The RGBA components are specified as numbers between 0 and 1.

Example

color.rgba 0.2, 0.4, 0.3, 0.5
# -> Color {r: 0.2, g: 0.4, b: 0.3, a: 0.5}

Color

The color modules core color type, represented by RGBA components.

All arithemetic operations are implemented, accepting colors or numbers as input. The color's RGBA components are iterable.

Example

color('red') + color('lime')
# -> Color {r: 1, g: 1, b: 0, a: 1}

r, g, b = color('yellow')
r, g, b
# -> (1.0, 1.0, 0.0)

Color.r | Color.red

|Color| -> Number

Returns the color's red component.

Example

color('black').r()
# -> 0.0

color('yellow').red()
# -> 1.0

Color.g | Color.green

|Color| -> Number

Returns the color's green component.

Example

color('black').g()
# -> 0.0

color('yellow').green()
# -> 1.0

Color.b | Color.blue

|Color| -> Number

Returns the color's blue component.

Example

color('black').b()
# -> 0.0

color('cyan').blue()
# -> 1.0

Color.b | Color.blue

|Color| -> Number

Returns the color's blue component.

Example

color('black').b()
# -> 0.0

color('cyan').blue()
# -> 1.0

Color.a | Color.alpha

|Color| -> Number

Returns the color's alpha component.

Example

color('black').a()
# -> 1.0

color(1, 1, 1, 0.5).alpha()
# -> 0.5

Color.set_r | Color.set_red

|Color, Number| -> Color

Sets the color's red component, and returns the color.

Example

color('black').set_r(1.0)
# -> Color {r: 1, g: 0, b: 0, a: 1}

color('red').set_red(0.0)
# -> Color {r: 0, g: 0, b: 0, a: 1}

Color.set_g | Color.set_green

|Color, Number| -> Color

Sets the color's green component, and returns the color.

Example

color('black').set_g(1.0)
# -> Color {r: 0, g: 1, b: 0, a: 1}

color('red').set_green(1.0)
# -> Color {r: 1, g: 1, b: 0, a: 1}

Color.set_b | Color.set_blue

|Color, Number| -> Color

Sets the color's blue component, and returns the color.

Example

color('black').set_b(1.0)
# -> Color {r: 0, g: 0, b: 1, a: 1}

color('red').set_blue(1.0)
# -> Color {r: 1, g: 0, b: 1, a: 1}

Color.set_a | Color.set_alpha

|Color, Number| -> Color

Sets the color's blue component, and returns the color.

Example

color('black').set_a(0.5)
# -> Color {r: 0, g: 0, b: 0, a: 0.5}

color('red').set_alpha(0.2)
# -> Color {r: 1, g: 0, b: 0, a: 0.2}

Color.mix

|Color, Color| -> Color

Returns a new color representing an even mix of the two input colors.

|Color, Color, Number| -> Color

Returns a new color representing a weighted mix of the two input colors.

The weight is specified by a number between 0 and 1, with values closer to 0 producing results closer to the first color, and values closer to 1 producing results closer to the second color.

Example

a, b = color('red'), color('blue')
a.mix b
# -> Color {r: 0.5, g: 0, b: 0.5, a: 1}

a.mix b, 0.25
# -> Color {r: 0.75, g: 0, b: 0.25, a: 1}