Cover for src/colors/color.cr


Lines
24 / 24 (100.00%)

1
require "json"
2
require "colorize"
3
require "./any_number"
4

        
5
module Colors
6
  class Color
7
1
    property red
8
1
    property green
9
1
    property blue
10

        
11
    def initialize(
12
      red : AnyNumber | ColorValue = ColorValue.off,
13
      green : AnyNumber | ColorValue = ColorValue.off,
14
      blue : AnyNumber | ColorValue = ColorValue.off
15
    )
16
488
      @red = ColorValue.new red
17
488
      @green = ColorValue.new green
18
488
      @blue = ColorValue.new blue
19
    end
20

        
21
    # Accepts a string like "#RRGGBB", returns a new Color. Raises an exception
22
    # if the string is not in that format.
23
    def self.from_s(string : String) : Color
24
4
      unless string[0] == '#'
25
1
        raise ArgumentError.new(
26
          "invalid character in color string at position 0: #{string[0]}"
27
        )
28
      end
29
3
      self.new(
30
        red: ColorValue.new(string[1..2]),
31
        green: ColorValue.new(string[3..4]),
32
        blue: ColorValue.new(string[5..6])
33
      )
34
    end
35

        
36
    def self.random
37
1
      rand self
38
    end
39

        
40
    # A `Color` with `@red` set to the given value and the other colors off.
41
    def self.red(intensity : AnyNumber | ColorValue = ColorValue.full)
42
2
      self.new red: ColorValue.new intensity
43
    end
44

        
45
    # A `Color` with `@blue` set to the given value and the other colors off.
46
    def self.blue(intensity : AnyNumber | ColorValue = ColorValue.full)
47
2
      self.new blue: ColorValue.new intensity
48
    end
49

        
50
    # A `Color` with `@green` set to the given value and the other colors off.
51
    def self.green(intensity : AnyNumber | ColorValue = ColorValue.full)
52
2
      self.new green: ColorValue.new intensity
53
    end
54

        
55
    # A zero `Color`.
56
    def self.black
57
3
      self.new
58
    end
59

        
60
    # :ditto:
61
    def self.zero
62
1
      black
63
    end
64

        
65
    # A `Color` with all values set to the maximum.
66
    def self.white
67
2
      self.grey ColorValue.max
68
    end
69

        
70
    # A `Color` where each primary color's value is equal.
71
    def self.grey(intensity : AnyNumber | ColorValue = ColorValue.new(0x88))
72
4
      intensity = ColorValue.new intensity
73
4
      self.new(red: intensity, blue: intensity, green: intensity)
74
    end
75

        
76
    # :ditto:
77
    def self.gray(intensity : AnyNumber | ColorValue = ColorValue.new(0x88))
78
1
      self.grey intensity
79
    end
80

        
81
    # Return the given text as a colorized value for display in terminals.
82
    def colorize(text : String)
83
1
      text.colorize(
84
        Colorize::ColorRGB.new(
85
          red: @red.to_u8,
86
          green: @green.to_u8,
87
          blue: @blue.to_u8
88
        )
89
      ).to_s
90
    end
91

        
92
    # Return the standard `#XXXXXX` hexadecimal representation of this `Color`
93
    def to_s
94
3
      "#" + @red.to_s + @green.to_s + @blue.to_s
95
    end
96

        
97
    def to_json
98
1
      %<"#{to_s}">
99
    end
100

        
101
    def to_json(builder)
102
1
      builder.string to_s
103
    end
104

        
105
    def self.new(pull parser : JSON::PullParser)
106
1
      self.from_s parser.read_string
107
    end
108
  end
109
end
110