struct Eagle::Color

Overview

An RGBA color with Float32 components from 0 to 1.

Colors tint everything you draw: Graphics#color, the color: argument on draw calls, Node2D#modulate, materials and lights. Multiplying a texture by a color tints it, so Color::WHITE leaves it unchanged.

g.color = Color.hex("#e3a537")
g.circle(100, 100, 20)
g.rect(10, 10, 50, 50, color: Color::RED.with_alpha(0.5)) # translucent

hurt = Color::WHITE.lerp(Color::RED, 0.8) # flash when damaged
rainbow = Color.hsv(Clock.elapsed * 90, 0.8, 1)
shadow = Color::BLACK.alpha(0.3)

Use Color.rgb for 0 to 255 byte values and Color.hex for web-style codes. Components aren't clamped until they reach the GPU, so values above 1 work for additive glow.

Defined in:

eagle/math/color.cr

Constant Summary

BLACK = Color.new(0, 0, 0)

Opaque black.

BLUE = Color.new(0, 0, 1)

Pure blue.

CORNFLOWER = Color.rgb(100, 149, 237)

Cornflower blue, the classic clear color.

CYAN = Color.new(0, 1, 1)

Pure cyan.

GRAY = Color.new(0.5, 0.5, 0.5)

50% gray.

GREEN = Color.new(0, 1, 0)

Pure green.

MAGENTA = Color.new(1, 0, 1)

Pure magenta.

ORANGE = Color.new(1, 0.5, 0)

Orange.

RED = Color.new(1, 0, 0)

Pure red.

TRANSPARENT = Color.new(0, 0, 0, 0)

Fully transparent. Use it to clear canvases.

WHITE = Color.new(1, 1, 1)

Opaque white. Draws textures untinted.

YELLOW = Color.new(1, 1, 0)

Pure yellow.

Constructors

Instance Method Summary

Constructor Detail

def self.gray(v : Number, a : Number = 1) : Color #

A gray of brightness v.


def self.hex(v : Int) : Color #

Creates a color from a number: 0xRRGGBB, or 0xRRGGBBAA when the value is larger than 24 bits.


def self.hex(s : String) : Color #

Creates a color from a web-style string: "#rgb", "#rgba", "#rrggbb" or "#rrggbbaa". The # is optional.


def self.hsv(h : Number, s : Number, v : Number, a : Number = 1) : Color #

Creates a color from hue in degrees (0 to 360, wraps), plus saturation and value in 0..1. Sweeping the hue is the easy way to get rainbows.


def self.new(r : Number, g : Number, b : Number, a : Number = 1) #

Creates a color from components in 0..1. Alpha defaults to opaque.


def self.new #

Creates opaque white.


def self.rgb(r : Int, g : Int, b : Int, a : Int = 255) : Color #

Creates a color from 0..255 byte values.

Color.rgb(255, 136, 0) # orange

def self.zero : Color #

Fully transparent black. Exists so Enumerable#sum works on colors.


Instance Method Detail

def *(o : Color) : Color #

Component-wise multiply, which is how tinting works.


def *(s : Number) : Color #

Scales red, green and blue by s and keeps alpha. Values above 1 brighten.


def +(o : Color) : Color #

Component-wise addition, including alpha.


def ==(o : Color) : Bool #

Exact equality.


def a : Float32 #

Alpha (opacity), from 0 (invisible) to 1 (opaque).


def a=(a : Float32) #

Alpha (opacity), from 0 (invisible) to 1 (opaque).


def alpha(a : Number) : Color #

Shorter name for #with_alpha.


def b : Float32 #

Blue, from 0 to 1.


def b=(b : Float32) #

Blue, from 0 to 1.


def darken(t : Number) : Color #

Blends toward black by t and keeps alpha.


def g : Float32 #

Green, from 0 to 1.


def g=(g : Float32) #

Green, from 0 to 1.


def lerp(o : Color, t : Number) : Color #

Blends toward o by t, including alpha.


def lighten(t : Number) : Color #

Blends toward white by t and keeps alpha.


def premultiplied : Color #

Color with red, green and blue multiplied by alpha, for premultiplied blending.


def r : Float32 #

Red, from 0 to 1.


def r=(r : Float32) #

Red, from 0 to 1.


def to_bytes : Tuple(UInt8, UInt8, UInt8, UInt8) #

The four channels as bytes, clamped to 0..255.


def to_rgba8 : UInt32 #

Packs the color into 0xRRGGBBAA, clamped to 0..255.


def to_s(io : IO) : Nil #
Description copied from struct Struct

Same as #inspect(io).


def to_vec4 : Vec4 #

The color as a Vec4, for shader uniforms.


def with_alpha(a : Number) : Color #

The same color with a different alpha.