From 1c3a84a5a4c4242906954c1dc5a0c3e6bd7aa356 Mon Sep 17 00:00:00 2001 From: emmanuel-toast Date: Tue, 12 Sep 2023 11:08:23 +1200 Subject: [PATCH] adding a new field to allow the user to explicitly determine the colour of text on top of the theme colour when used as a background --- src/Models/ThemeColour.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Models/ThemeColour.php b/src/Models/ThemeColour.php index 59f3c28..870cf8e 100644 --- a/src/Models/ThemeColour.php +++ b/src/Models/ThemeColour.php @@ -10,6 +10,7 @@ use SilverStripe\Security\Security; use SilverStripe\Control\Controller; use SilverStripe\Forms\LiteralField; +use SilverStripe\Forms\OptionsetField; use SilverStripe\Forms\RequiredFields; use Toast\ThemeColours\Helpers\Helper; use SilverStripe\SiteConfig\SiteConfig; @@ -26,6 +27,7 @@ class ThemeColour extends DataObject 'Title' => 'Varchar(255)', 'CustomID' => 'Varchar(255)', 'Colour' => 'Color', + 'ThemeColourTextColour' => 'Varchar(30)' ]; private static $belongs_many_many = [ @@ -57,6 +59,10 @@ public function getCMSFields() ColorField::create('Colour', 'Colour') ->setReadOnly(!$this->canChangeColour()) ->setDescription($this->canChangeColour() ? 'Please select a colour' : 'This is the default theme colour "' . $this->CustomID . '" and cannot be changed.'), + OptionsetField::create('ThemeColourTextColour', 'Default Text Appearance', [ + 'light' => 'Light', + 'dark' => 'Dark' + ])->setDescription('Select the text colour to be used when this theme colour is used as a background. If left unselected, a calculation will be made to determine the best text colour for legibility.') ]); } else { // Hide the CustomID field @@ -198,6 +204,12 @@ public function getColourHexCode() // Method to return Brightness public function getColourBrightness() { + // First let's check if the ThemeColourTextColour is set + if ($this->ThemeColourTextColour) { + // If it is, return that + return $this->ThemeColourTextColour; + } + $hex = $this->Colour ?: 'ffffff'; $r = hexdec(substr($hex,0,2)); $g = hexdec(substr($hex,2,2));