Class: Gitlab::Color
- Inherits:
-
Object
- Object
- Gitlab::Color
- Defined in:
- lib/gitlab/color.rb
Defined Under Namespace
Modules: Constants
Constant Summary collapse
- PATTERN =
/\A\#(?:[0-9A-Fa-f]{3}){1,2}\Z/.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(_options = nil) ⇒ Object
- #contrast ⇒ Object
- #eql(other) ⇒ Object (also: #==)
-
#initialize(value) ⇒ Color
constructor
A new instance of Color.
- #light? ⇒ Boolean
- #luminosity ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(value) ⇒ Color
Returns a new instance of Color.
7 8 9 |
# File 'lib/gitlab/color.rb', line 7 def initialize(value) @value = value&.strip&.freeze end |
Class Method Details
.of(color) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/gitlab/color.rb', line 165 def self.of(color) raise ArgumentError, 'No color spec' unless color return color if color.is_a?(self) color = color.to_s.strip Constants::COLOR_NAME_TO_HEX[color.downcase] || new(color) end |
Instance Method Details
#as_json(_options = nil) ⇒ Object
177 178 179 |
# File 'lib/gitlab/color.rb', line 177 def as_json( = nil) to_s end |
#contrast ⇒ Object
202 203 204 205 206 |
# File 'lib/gitlab/color.rb', line 202 def contrast return Constants::DARK if light? Constants::LIGHT end |
#eql(other) ⇒ Object Also known as: ==
181 182 183 184 185 |
# File 'lib/gitlab/color.rb', line 181 def eql(other) return false unless other.is_a?(self.class) to_s == other.to_s end |
#light? ⇒ Boolean
192 193 194 |
# File 'lib/gitlab/color.rb', line 192 def light? valid? && rgb.sum > 500 end |
#luminosity ⇒ Object
196 197 198 199 200 |
# File 'lib/gitlab/color.rb', line 196 def luminosity return :light if light? :dark end |
#to_s ⇒ Object
173 174 175 |
# File 'lib/gitlab/color.rb', line 173 def to_s @value.to_s end |
#valid? ⇒ Boolean
188 189 190 |
# File 'lib/gitlab/color.rb', line 188 def valid? PATTERN.match?(@value) end |