4
1
MAX_INTENSITY = 255_u8
7
# The value one primary color has out of a RGB color.
9
1007
def initialize(@value : UInt8 = MIN_INTENSITY); end
11
# Create a `ColorValue` from a number or another color value.
13
# Raises `OverflowError` if the value won't fit in a `UInt8`; I.E. if the
14
# value is less than 0 or greater than 255, or not an integer; E.G. `3.0`,
15
# `100u64`, `255f64`, or `ColorValue::new(100u8)` are acceptable
16
# parameters, `-1`, `256u16`, or `123.456` will throw.
17
def initialize(value : AnyNumber | ColorValue)
18
3721
@value = value.to_u8
21
# Create a `ColorValue` from a string representation: either a
22
# **hexadecimal** numeric value between 0 and 255, or the words `"full"` or
23
# `"off"` which translate to `MAX_INTENSITY` and `MIN_INTENSITY`,
26
# Raises if the value isn't a valid **base-16** integer which fits into 8
28
def initialize(value : String = "0")
30
2
@value = MAX_INTENSITY
31
19
elsif value == "off"
32
2
@value = MIN_INTENSITY
34
17
@value = value.to_i(base: 16).to_u8
39
31
ColorValue.new MIN_INTENSITY
43
13
ColorValue.new MAX_INTENSITY
47
4
ColorValue.new MAX_INTENSITY
51
2
ColorValue.new MIN_INTENSITY
55
25
io.printf "%02X", @value
71
5
{% for op in [:==, :>, :<, :<=, :>=] %}
73
@value {{op.id}} other.to_u8
77
# regular and bitwise operators
78
8
{% for op in [:-, :+, :*, :/, :%, :&, :|, :"^"] %}
79
# Defines a new color where this color {{op.id}} other
81
ColorValue.new( @value {{ op.id }} other )
86
2
@value # unsigned so always positive