-
Notifications
You must be signed in to change notification settings - Fork 109
/
Color.swift
99 lines (89 loc) · 3.14 KB
/
Color.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Color.swift
// Rainbow
//
// Created by Wei Wang on 15/12/22.
//
// Copyright (c) 2018 Wei Wang <onevcat@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
public typealias RGB = (UInt8, UInt8, UInt8)
public enum ColorType: ModeCode {
case named(Color)
case bit8(UInt8)
case bit24(RGB)
@available(*, deprecated, message: "For backward compatibility.")
var namedColor: Color? {
switch self {
case .named(let color): return color
default: return nil
}
}
public var value: [UInt8] {
switch self {
case .named(let color): return color.value
case .bit8(let color): return [ControlCode.setColor, ControlCode.set8Bit, color]
case .bit24(let rgb): return [ControlCode.setColor, ControlCode.set24Bit, rgb.0, rgb.1, rgb.2]
}
}
public var toBackgroundColor: BackgroundColorType {
switch self {
case .named(let color): return .named(color.toBackgroundColor)
case .bit8(let color): return .bit8(color)
case .bit24(let rgb): return .bit24(rgb)
}
}
}
extension ColorType: Equatable {
public static func == (lhs: ColorType, rhs: ColorType) -> Bool {
switch (lhs, rhs) {
case (.named(let color1), .named(let color2)): return color1 == color2
case (.bit8(let color1), .bit8(let color2)): return color1 == color2
case (.bit24(let color1), .bit24(let color2)): return color1 == color2
default: return false
}
}
}
public typealias Color = NamedColor
/// Valid named text colors to use in `Rainbow`.
public enum NamedColor: UInt8, ModeCode, CaseIterable {
case black = 30
case red
case green
case yellow
case blue
case magenta
case cyan
case white
case `default` = 39
case lightBlack = 90
case lightRed
case lightGreen
case lightYellow
case lightBlue
case lightMagenta
case lightCyan
case lightWhite
public var value: [UInt8] {
return [rawValue]
}
public var toBackgroundColor: NamedBackgroundColor {
return NamedBackgroundColor(rawValue: rawValue + 10)!
}
}