Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add lighten and darken functions to colors

Oliver Günther8819035bc651
diff --git a/app/helpers/colors_helper.rb b/app/helpers/colors_helper.rb
index e4d0a284d26..179e01651e0 100644
--- a/app/helpers/colors_helper.rb
+++ b/app/helpers/colors_helper.rb
@@ -49,15 +49,6 @@ module ColorsHelper
colored_thing.color_id
end
- def darken_color(hex_color, amount = 0.4)
- hex_color = hex_color.delete('#')
- rgb = hex_color.scan(/../).map(&:hex)
- rgb[0] = (rgb[0].to_i * amount).round
- rgb[1] = (rgb[1].to_i * amount).round
- rgb[2] = (rgb[2].to_i * amount).round
- "#%02x%02x%02x" % rgb
- end
-
def colored_text(color)
background = color.contrasting_color(dark_color: '#333', light_color: 'transparent')
style = "background-color: #{background}; color: #{color.hexcode}"
diff --git a/app/helpers/mail_notification_helper.rb b/app/helpers/mail_notification_helper.rb
index ae3f4ab3578..1af12cab37b 100644
--- a/app/helpers/mail_notification_helper.rb
+++ b/app/helpers/mail_notification_helper.rb
@@ -43,7 +43,7 @@ module MailNotificationHelper
color_id = selected_color(type)
if color_id
color = Color.find(color_id)
- return color.super_bright? ? darken_color(color.hexcode, 0.75) : color.hexcode
+ return color.super_bright? ? color.darken(0.75) : color.hexcode
end
default_fallback
diff --git a/app/models/color.rb b/app/models/color.rb
index 38d0bf54bf8..602002252b1 100644
--- a/app/models/color.rb
+++ b/app/models/color.rb
@@ -27,10 +27,13 @@
#++
class Color < ApplicationRecord
+ include ::Colors::HexColor
+
self.table_name = 'colors'
- has_many :planning_element_types, class_name: 'Type',
- dependent: :nullify
+ has_many :planning_element_types,
+ class_name: 'Type',
+ dependent: :nullify
before_validation :normalize_hexcode
@@ -38,74 +41,4 @@ class Color < ApplicationRecord
validates :name, length: { maximum: 255, unless: lambda { |e| e.name.blank? } }
validates :hexcode, format: { with: /\A#[0-9A-F]{6}\z/, unless: lambda { |e| e.hexcode.blank? } }
-
- ##
- # Returns the best contrasting color, either white or black
- # depending on the overall brightness.
- def contrasting_color(light_color: '#FFFFFF', dark_color: '#333333')
- if bright?
- dark_color
- else
- light_color
- end
- end
-
- ##
- # Get the fill style for this color.
- # If the color is light, use a dark font.
- # Otherwise, use a white font.
- def color_styles(light_color: '#FFFFFF', dark_color: '#333333')
- if bright?
- { color: dark_color, 'background-color': hexcode }
- else
- { color: light_color, 'background-color': hexcode }
- end
- end
-
- ##
- # Returns whether the color is bright according to
- # YIQ lightness.
- def bright?
- brightness_yiq >= 150
- end
-
- ##
- # Returns whether the color is very bright according to
- # YIQ lightness.
- def super_bright?
- brightness_yiq >= 200
- end
-
- ##
- # Sum the color values of each channel
- # Same as in frontend color-contrast.functions.ts
- def brightness_yiq
- r, g, b = rgb_colors
- ((r * 299) + (g * 587) + (b * 114)) / 1000
- end
-
- ##
- # Splits the hexcode into rbg color array
- def rgb_colors
- hexcode
- .gsub('#', '') # Remove trailing #
- .scan(/../) # Pair hex chars
- .map { |c| c.hex } # to int
- end
-
- protected
-
- def normalize_hexcode
- if hexcode.present? and hexcode_changed?
- self.hexcode = hexcode.strip.upcase
-
- unless hexcode.starts_with? '#'
- self.hexcode = '#' + hexcode
- end
-
- if hexcode.size == 4 # =~ /#.../
- self.hexcode = hexcode.gsub(/([^#])/, '\1\1')
- end
- end
- end
end
diff --git a/app/models/colors/hex_color.rb b/app/models/colors/hex_color.rb
new file mode 100644
index 00000000000..ab21d06673a
--- /dev/null
+++ b/app/models/colors/hex_color.rb
@@ -0,0 +1,94 @@
+module Colors
+ module HexColor
+ ##
+ # Returns the best contrasting color, either white or black
+ # depending on the overall brightness.
+ def contrasting_color(light_color: '#FFFFFF', dark_color: '#333333')
+ if bright?
+ dark_color
+ else
+ light_color
+ end
+ end
+
+ ##
+ # Get the fill style for this color.
+ # If the color is light, use a dark font.
+ # Otherwise, use a white font.
+ def color_styles(light_color: '#FFFFFF', dark_color: '#333333')
+ if bright?
+ { color: dark_color, 'background-color': hexcode }
+ else
+ { color: light_color, 'background-color': hexcode }
+ end
+ end
+
+ ##
+ # Returns whether the color is bright according to
+ # YIQ lightness.
+ def bright?
+ brightness_yiq >= 150
+ end
+
+ ##
+ # Returns whether the color is very bright according to
+ # YIQ lightness.
+ def super_bright?
+ brightness_yiq >= 200
+ end
+
+ ##
+ # Sum the color values of each channel
+ # Same as in frontend color-contrast.functions.ts
+ def brightness_yiq
+ r, g, b = rgb_colors
+ ((r * 299) + (g * 587) + (b * 114)) / 1000
+ end
+
+ ##
+ # Splits the hexcode into rbg color array
+ def rgb_colors
+ hexcode
+ .gsub('#', '') # Remove trailing #
+ .scan(/../) # Pair hex chars
+ .map(&:hex) # to int
+ end
+
+ def rgb_modify(&)
+ rgb_colors
+ .map(&)
+ .map(&:round)
+ .map { |val| [val, 255].min }
+ end
+
+ ##
+ # Darken this color by the given decimal amount
+ def darken(amount)
+ r, g, b = rgb_modify { |color| color * amount }
+ '#%<r>02x%<g>2x%<b>02x' % { r:, g:, b: }
+ end
+
+ ##
+ # Lighten this color by the given decimal amount
+ def lighten(amount)
+ r, g, b = rgb_modify { |color| color + (255 * amount) }
+ '#%<r>02x%<g>2x%<b>02x' % { r:, g:, b: }
+ end
+
+ # rubocop:disable Metrics/AbcSize
+ def normalize_hexcode
+ return unless hexcode.present? && hexcode_changed?
+
+ self.hexcode = hexcode.strip.upcase
+
+ unless hexcode.starts_with? '#'
+ self.hexcode = "##{hexcode}"
+ end
+
+ if hexcode.size == 4 # =~ /#.../
+ self.hexcode = hexcode.gsub(/([^#])/, '\1\1')
+ end
+ end
+ # rubocop:enable Metrics/AbcSize
+ end
+end
diff --git a/app/models/design_color.rb b/app/models/design_color.rb
index 1fffdc55dd6..ac78ceae30e 100644
--- a/app/models/design_color.rb
+++ b/app/models/design_color.rb
@@ -27,6 +27,8 @@
#++
class DesignColor < ApplicationRecord
+ include ::Colors::HexColor
+
before_validation :normalize_hexcode
after_commit -> do
# CustomStyle.current.updated_at determines the cache key for inline_css
@@ -61,21 +63,4 @@ class DesignColor < ApplicationRecord
end
end
end
-
- protected
-
- # This could be DRY! This method is taken from model Color.
- def normalize_hexcode
- if hexcode.present? and hexcode_changed?
- self.hexcode = hexcode.strip.upcase
-
- unless hexcode.starts_with? '#'
- self.hexcode = '#' + hexcode
- end
-
- if hexcode.size == 4 # =~ /#.../
- self.hexcode = hexcode.gsub(/([^#])/, '\1\1')
- end
- end
- end
end