From a1e7a2d740da1fbca12b04885484b85eb5ecffe3 Mon Sep 17 00:00:00 2001 From: Alexander Brandon Coles Date: Thu, 14 Aug 2025 19:49:44 +0100 Subject: [PATCH] [#66557] Make #rgb_colors handle invalid hexcodes Ensures dependent methods such as `#darken` do not blow up if a hexcode value is invalid (which can occur if the record is unpersisted or fails validation). Missing/invalid R, G, B component values will now be handled as zero. --- app/models/colors/hex_color.rb | 6 +- spec/models/colors/hex_color_spec.rb | 89 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 spec/models/colors/hex_color_spec.rb diff --git a/app/models/colors/hex_color.rb b/app/models/colors/hex_color.rb index 8d0a402a3c9..49f1073888a 100644 --- a/app/models/colors/hex_color.rb +++ b/app/models/colors/hex_color.rb @@ -76,11 +76,13 @@ module Colors end ## - # Splits the hexcode into rbg color array + # Splits the hexcode into rgb color array def rgb_colors hexcode - .delete("#") # Remove trailing # + .delete_prefix("#") # Remove leading # + .ljust(6, "0") # Pad to at least 6 chars .scan(/../) # Pair hex chars + .first(3) .map(&:hex) # to int end diff --git a/spec/models/colors/hex_color_spec.rb b/spec/models/colors/hex_color_spec.rb new file mode 100644 index 00000000000..2560a066b9a --- /dev/null +++ b/spec/models/colors/hex_color_spec.rb @@ -0,0 +1,89 @@ +# frozen_string_literal: true + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See COPYRIGHT and LICENSE files for more details. +#++ + +require "spec_helper" + +RSpec.describe Colors::HexColor do + let(:color_class) { Data.define(:hexcode).include(described_class) } + + describe "#rgb_colors" do + context "when hexcode is valid" do + it "returns an RGB array" do + expect(color_class.new(hexcode: "#FF69B4").rgb_colors).to eq [255, 105, 180] + end + end + + context "when hexcode is too short" do + it "returns an RGB array, zeroing missing component values" do + expect(color_class.new(hexcode: "#ff").rgb_colors).to eq [255, 0, 0] + end + end + + context "when hexcode is too long" do + it "returns an RGB array, dropping extra values" do + expect(color_class.new(hexcode: "#ff3366AA").rgb_colors).to eq [255, 51, 102] + end + end + + context "when hexcode is invalid" do + it "returns an RGB array, zeroing invalid component values" do + expect(color_class.new(hexcode: "#eeXXcd").rgb_colors).to eq [238, 0, 205] + end + end + end + + describe "#darken" do + context "when hexcode is valid" do + it "returns the darkened color's hexcode" do + expect(color_class.new(hexcode: "#663399").darken(0.5)).to eq "#331a4d" + end + end + + context "when hexcode is invalid" do + it "zeroes missing component values and returns the darkened color's hexcode" do + expect(color_class.new(hexcode: "#eeXXcd").darken(0.5)).to eq "#770067" + end + end + end + + describe "#lighten" do + context "when hexcode is valid" do + it "returns the lightened color's hexcode" do + expect(color_class.new(hexcode: "#663399").lighten(0.5)).to eq "#b399cc" + end + end + + context "when hexcode is invalid" do + it "zeroes missing component values and returns the darkened color's hexcode" do + expect(color_class.new(hexcode: "#eeXXcd").lighten(0.5)).to eq "#f780e6" + end + end + end +end