Class: Gitlab::Color

Inherits:
Object
  • Object
show all
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

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

Raises:

  • (ArgumentError)

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(_options = nil)
  to_s
end

#contrastObject


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

Returns:

  • (Boolean)

192
193
194
# File 'lib/gitlab/color.rb', line 192

def light?
  valid? && rgb.sum > 500
end

#luminosityObject


196
197
198
199
200
# File 'lib/gitlab/color.rb', line 196

def luminosity
  return :light if light?

  :dark
end

#to_sObject


173
174
175
# File 'lib/gitlab/color.rb', line 173

def to_s
  @value.to_s
end

#valid?Boolean

Returns:

  • (Boolean)

188
189
190
# File 'lib/gitlab/color.rb', line 188

def valid?
  PATTERN.match?(@value)
end