Issue Summary Update (#78)

Problem/Motivation

Once Bartik will be deprecated, the color module will also be removed from core and can live on in a contributed project, as decided in #2808151-55: [policy] Move the Color module to a contributed project when Bartik is deprecated (comments 55 - 70).

To avoid each theme developer to invent their own system and to give color in contrib a chance, we need an API for extracting the editable colors from a theme. Once we have such system in place, we can hopefully convince the Olivero team to tap into it as an example for other themes.

Proposed resolution

Create a system where theme developers can add a THEMENAME.colors.yml file and automatically have all the fields they need added to the theme's settings form.

Remaining tasks

  1. Currently adding the new colors.yml file will only allow theme developers to parse the colors. Adding the color options to the theme's settings form would still be an additional step. It might be a good idea auto-populate those color fields on the form by default?
    An example of a working implemention (form and CSS variable injection) based on this method can be found in the following sandbox: https://www.drupal.org/sandbox/neograph734/3279654.
  2. Colors can only be added & stored as hex codes (#a1b2c3). Olivero currently converts those colors to HSL format in a preprocess, but it may be more performant to store colors in new schema types (as recommended by @alexpott on the Olivero implementation). We should be able to store colors in hexadecimal, rgb(a) and hsl(a) at a minimum. Other color spaces may be worth adding as well.
  3. Color conversion is a complex mathematical formula, but there are PHP and JS libraries avaialble that can handle conversion tasks for us and help theme developers. Potential options include spatie/color for PHP and TinyColor for JS.

User interface changes

Theme settings forms will have new fields generated for them automatically based on the schemes & colors outlined in a theme's THEMENAME.colors.yml

Dependency Evaluation

Below is the beginning of the dependency eval process for spatie/color:

  1. Maintainership of the package

    Color is maintained by a Belgian Laravel agency, Spatie. The company has a strong history of open source. The package was created in fall of 2016, and was last updated December 2022. There are three open issues in the issue queue, all enhancements, from 2017. This package has ~830k downloads on packagist and ~280 stars on github.

  2. Security policies of the package

    The project's README states, "If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker."

  3. Expected release and support cycles

    Project releases do not appear to have a regular cadence, but the need served by the project is fairly small and unchanging. Releases are outlined on the project's github page, and there were multiple releases in each of 2020, 2021, and 2022.

  4. Code quality

    I do not know how to evaluate this, but I didn't have any issues reading the code and understanding what was available.

  5. Other dependencies it would add, if any (the full tree, not just direct dependencies), and evaluations for those dependencies as well

    spatie/color only lists two development dependencies for testing purposes, "pestphp/pest" and "phpunit/phpunit".


Orignal issue

The color module is built into core. All themes have a .info file. Why should they have a color.inc $info to define the color set?

As a proof of concept I copied Bartik as Bartwo and moved the color definitions from color.inc to bartwo.info and made a slight enhancement to color.module. The result is a nice color set in the theme settings. This would be a nice feature and is backward compatible. The change lets the existing stuff happen uninterrupted and just adds an extra step to bring in colors from .info. you can change a theme over at any time.

There are other changes out there to expand the range of colors in a set and some other fine enhancements, all compatible with the change I tried. Moving the initial colors into the .info file would be one step toward reducing the code for a theme.

Issue fork drupal-788332

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tim.plunkett’s picture

Version: 7.x-dev » 8.x-dev

Likely not backportable, but interesting idea.

markhalliwell’s picture

This is actually very possible with drupal_parse_info_file() (which is in D7 too).

I do think this is worth getting in (seriously a much better way to manage color stuff), however this technically would be considered an "API change".

markhalliwell’s picture

Issue tags: +Needs reroll

This patch needs a reroll/refactor.

markhalliwell’s picture

Version: 8.x-dev » 9.x-dev
Status: Active » Postponed
Issue tags: -API change, -Needs backport to D7

We're past code freeze, postponing to 9.x.

markhalliwell’s picture

Title: Color could use .info instead of PHP code » Convert color.inc into THEME.color.yml

Was talking with alexpott about this and this seems to make the most sense. We could leverage the new config entities for this as well.

catch’s picture

Version: 9.x-dev » 8.3.x-dev
Issue summary: View changes
Status: Postponed » Active

This could be done with bc in a minor release.

tim.plunkett’s picture

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

anish.a’s picture

Issue tags: -Needs reroll

I can't find any color.inc in 8.4.x branch. Can someone please clarify whether this issue is relevant anymore? If its not, we can close it.

Removing reroll tag as its not a reroll anymore.

anish.a’s picture

Issue tags: +Needs reroll

I was mistaken. The color.inc is in individual themes (like bartic)

jofitz’s picture

Status: Active » Needs review
Issue tags: -Needs reroll
StatusFileSize
new2.52 KB

Just to get this off the "Needs Reroll" list!

Jeff Burnz’s picture

Sure, as long as it's BC, all good. I have a lot of colorable themes in the wild.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

borisson_’s picture

Status: Needs review » Needs work

This will also need tests, but instead of fixing the nitpicks, shouldn't we start with creating an example of an expected .color.yml file?

  1. --- a/core/modules/color/color.module
    +++ b/core/modules/color/color.module
    
    @@ -164,8 +165,55 @@ function color_get_info($theme) {
    +//    return $info;
    +  }
    

    Let's just remove this line if we no longer need it, having it in comment doesn't seem like a great plan.

  2. +++ b/core/modules/color/color.module
    @@ -164,8 +165,55 @@ function color_get_info($theme) {
    +  /* PeterX */
    

    What does this do? Should we not remove this?

  3. +++ b/core/modules/color/color.module
    @@ -164,8 +165,55 @@ function color_get_info($theme) {
    +  $file = DRUPAL_ROOT . '/' . $path . '/' . $theme . '.info';
    +  $info_parser = new InfoParser();
    +  $info = $info_parser->parse($file);
    

    #5 mentions that this should be in a theme.color.yml file, this is looking for a .info.yml file.

    Which one is correct?

  4. +++ b/core/modules/color/color.module
    @@ -164,8 +165,55 @@ function color_get_info($theme) {
    +  if (!empty($info['color'])) {
    

    Let's invert this if to make the code easier to grok.

    if (empty($info['color']) {
      return;
    }
    
  5. +++ b/core/modules/color/color.module
    @@ -164,8 +165,55 @@ function color_get_info($theme) {
    +// The following functions to be refined based on what is legal in .info files.
    +function _color_get_info_clean($string) {
    +  return str_replace(' ', '_', strtolower($string));
    +}
    +
    +function _color_get_info_color_clean($string) {
    +  return '#' . str_replace(array('_', '#'), '', _color_get_info_clean($string));
     }
    

    If we need this additional validation, let's move it to some kind of utility class to make testing this possible.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new1.49 KB

To make reuse color names outside of drupal better to split metadata (put to theme's .info.yml file)
and palette colors (each palette has own file) otoh maybe better to put colors in single theme_name.color.yml
The single fine is easy to discover with core's \Drupal\Core\Plugin\Discovery\YamlDiscovery

YurkinPark’s picture

StatusFileSize
new2.59 KB
new1.05 KB

I've fixed some notices pointed in comment #15 and i've prepared example of how can we use color.yml file during this modification.
Also, i have some idea that we can provide just one css file which will contains colors only. Based on my example this file can be looks like this

:root {
  --top: #055a8e;
  --bottom: #1d84c3;
  --bg: #ffffff;
  --sidebarborders: #f6f6f2;
  --footer: #292929;
  --titleslogan: #fffeff;
  --text: #3b3b3b;
  --link: #0071b3;
}

Possibly this is another task, but i'm sure generating of this file can be used by some option inside .color.yml file

Status: Needs review » Needs work

The last submitted patch, 18: 788332-18.patch, failed testing. View results

andypost’s picture

@YurkinPark looks you made patch not from core's git repo

YurkinPark’s picture

Status: Needs work » Needs review
StatusFileSize
new2.59 KB
new4.26 KB

Seems like yes, i've selected wrong directory during patch generating, attaching new one and interdiff for patch #11

Status: Needs review » Needs work

The last submitted patch, 21: 788332-21.patch, failed testing. View results

YurkinPark’s picture

StatusFileSize
new2.65 KB
new4.22 KB

and again, sorry

YurkinPark’s picture

Status: Needs work » Needs review

The last submitted patch, color.module.patch, failed testing. View results

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

andypost’s picture

Version: 8.9.x-dev » 9.0.x-dev
xjm’s picture

Version: 9.0.x-dev » 9.1.x-dev

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

neograph734’s picture

Issue summary: View changes

Updating this issue after the discussion held in #2808151-55: [policy] Move the Color module to a contributed project when Bartik is deprecated. Once Bartik will be deprecated, the color module will also be removed from core and can live on in a contributed project.

This is making this issue more relevant because currently there is no standardized way for themes to expose their colors to Drupal. Only the Olivero theme might have changeable colors #3257274: Implement color changing theme settings for Olivero but the proposal is built within the theme settings and the colors and schemes are defined in JavaScript (merge request).
To avoid each theme developer to invent their own system and to give color in contrib a chance, we need an API for extracting the editable colors from a theme.

I'll start playing around with the idea's already proposed here.

neograph734’s picture

Version: 9.4.x-dev » 9.5.x-dev
StatusFileSize
new2.97 KB

So here is my take on this.

I am struggling a bit on how much of an API this should become and if we should include functions for variable injection back into the render system as well. Looking at what Olivero currently does in their proposal is converting from hex to HSL and using that. So I believe we should not force a system.
The absolute minimum is a parser for retrieving the colors and color schemes from a theme.

Usage:

$parser = new Drupal\Core\Theme\ThemeColorsParser();
$file = \Drupal::service('extension.list.theme')->getPath('olivero') . '/olivero.colors.yml';
$colors = $parser->parse($file);

Result:

Array
(
    [colors] => Array
        (
            [base_primary_color] => Primary base color
        )

    [schemes] => Array
        (
            [default] => Array
                (
                    [label] => Blue Lagoon
                    [colors] => Array
                        (
                            [base_primary_color] => #1b9ae4
                        )

                )

            [firehouse] => Array
                (
                    [label] => Firehouse
                    [colors] => Array
                        (
                            [base_primary_color] => #a30f0f
                        )

                )

        )

)

Key differences from earlier patches:
- I chose the name theme.colors.yml. The .breakpoints.yml and .libraries.yml are also in plural form, so I believe it makes sense to use colors too.
- The parser is moved to the Drupal\Core\Theme namespace because it applies to themes only.
- Documentation on how this can be used is added.
- Instead of Bartik, which will be deprecated, I have included a yml file for the Olivero theme instead.

Follow up:
- Streamline with the Olivero team so that they can tap into this system.
- Do we need a change record for this?

I believe this will help in moving color to contrib and standardising the way themes can define customizable colors.
Any thoughts?

neograph734’s picture

Title: Convert color.inc into THEME.color.yml » Provide a parser for THEME.colors.yml
Component: color.module » theme system

Updating the name and moving this from color module to the theme system.

andypost’s picture

It should be easier to create 2.x branch in dgo.to/color and work there

neograph734’s picture

@andypost, please read my reasoning in comment #65 in #2808151-65: [policy] Move the Color module to a contributed project when Bartik is deprecated and further.
In order to be able to build something in contrib we need a standardised way to extract this information. That is what I am trying to pursue here. Everything else (UI, variable injection, etc) can be built in a separate module.

ranjith_kumar_k_u’s picture

StatusFileSize
new2.97 KB
new549 bytes

Status: Needs review » Needs work

The last submitted patch, 38: 788332-38.patch, failed testing. View results

neograph734’s picture

Status: Needs work » Needs review

That was an unrelated fail. Setting back to NR. Thanks @ranjith_kumar_k_u for fixing the coding standard.

andypost’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Looks great! Also colors has different #fff vs hsl() implementation so providing this parser we need a validator for it (could be separate issue)

Also I'm not sure that core's namespace is good fit for this parser

+++ b/core/lib/Drupal/Core/Theme/ThemeColorsParser.php
@@ -0,0 +1,65 @@
+   * - schemes: Color schemes; An array of color schemes keyed by their scheme name.
...
+      'schemes' => [],

I prefer palette wording

neograph734’s picture

Thanks for the feedback andypost! Sharing my thoughts below.

Also colors has different #fff vs hsl() implementation so providing this parser we need a validator for it (could be separate issue)

Do we? An HTML color element only accepts a HEX value (documentation) and in core.data_types.schema.yml there is a color_hex definition.
I am tempted to say that all colors should always be defined as hex and can be converted to HSL before rendering (forms and pages), which would be for Color2 module.
Olivero does the same in #3257274: Implement color changing theme settings for Olivero.

Also I'm not sure that core's namespace is good fit for this parser

I am not that familiar with all the namespaces provided by Drupal, so I would need some guidance for a better place then.

I prefer palette wording

This is the more or less the same data structure as used in the current color module. A color scheme has a label and a palette of colors. To fully match the old structure the YAML would need to look like this:

colors:
  base_primary_color: 'Primary base color'

schemes:
  default:
    label: 'Blue Lagoon'
    palette:
      base_primary_color: '#1b9ae4'

But now we are rewriting I felt that the extra term would not be needed.

I'll see if I can write a test for this.

andy-blum’s picture

@Neograph734 Thanks for writing this - I'm looking at it now for integration with the Olivero color picker issue you referenced.

One thing I'd like to shine a light on:

An HTML color element only accepts a HEX value (documentation) and in core.data_types.schema.yml there is a color_hex definition.
I am tempted to say that all colors should always be defined as hex and can be converted to HSL before rendering (forms and pages), which would be for Color2 module.

This is true, the HTML color input does only support HEX, but we're currently converting hex to hsl on every single page load. As @alexpott points out: "Also I think given we're storing this in config I think maybe we should be storing the hsl value and not the hex. Since we need to call this for every html page served."

Would it be possible to utilize/mimic a library like spatie/color (MIT licensed) so we could do all the color code conversions prior to storage?

andy-blum’s picture

Also, and as a Front-End dev, I could be wrong here - should parse be a static method? How should I use this in my theme-settings.php file?

neograph734’s picture

Assigned: Unassigned » neograph734

@Andy, thanks for chiming in.

I am still searching a bit how much this parser should do. I am trying to keep it is universal as possible to be applicabe to Olivero, and a color2 module. I think a color conversion class could be a perfect utility addition to Dupal, but I believe that is should be separated from this parser. This should only extract adjustable colors and predefined color schemes.

Also, and as a Front-End dev, I could be wrong here - should parse be a static method? How should I use this in my theme-settings.php file?

I am currently figuring that out myself as well in a work-in-progress sandbox module.

My idea is to use the color variable definitions to build a form and use the scheme definititions in JavaScript to update the color input fields dynamically once the scheme selection changes. (But I am still writing the JavaScript.)

Self assigning to work on the test.

andy-blum’s picture

@neograph734 - I updated the Olivero color picker to work using this. See the MR from #3257274: Implement color changing theme settings for Olivero and apply patch #38 from here. You might find the color-picker.es6.js does what you need

saschaeggi’s picture

Excited to also use this in Gin soon! 🙏👏

andy-blum’s picture

@neograph734 I just updated the olivero branch again to remove the dependency on this issue, but if you'd like to test with Olivero you can checkout MR 1623 at commit e59be4f3

neograph734’s picture

Assigned: neograph734 » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new8.31 KB
new6.24 KB

I think something like this should suffice as a test.

I've also included some extra RuntimeExceptions. I did not know if a new type of Exception is allowed or needed.

neograph734’s picture

StatusFileSize
new8.31 KB

Well, that's some really strict checking... Omitting the interdiff due to the minimalism of the changes.

Status: Needs review » Needs work

The last submitted patch, 50: 788332-50.patch, failed testing. View results

neograph734’s picture

Status: Needs work » Needs review
StatusFileSize
new8.28 KB
new2.27 KB

This should make them all pass.

Status: Needs review » Needs work

The last submitted patch, 52: 788332-52.patch, failed testing. View results

neograph734’s picture

Status: Needs work » Needs review

Heh, my test passes, but it is the same test that caused #38 to fail the first time. Retesting.

neograph734’s picture

Version: 9.5.x-dev » 10.0.x-dev
StatusFileSize
new10.5 KB

Updating the patch to be compatible with Olivero now that #3257274: Implement color changing theme settings for Olivero has been committed. Big thanks to @andy-blum for #48 as that saved a lot of work.

Others interested in playing with this (@saschaeggi) you might want to take a look at my Color POC sandbox project as well. Implementation instructons are on the front page.

andypost’s picture

Project: Drupal core » Color backport
Version: 10.0.x-dev » 2.x-dev
Component: theme system » Code

I bet that's better/faster place to collaborate

neograph734’s picture

Project: Color backport » Drupal core
Version: 2.x-dev » 10.0.x-dev
Component: Code » theme system

@andypost Please read my reasoning in #2808151-59: [policy] Move the Color module to a contributed project when Bartik is deprecated and onward. We need to have some sort of API in Core otherwise each theme will implement color changing as they like, which will actually block any progress in contrib space.

Let's build a modern UI in the color backport project!

andypost’s picture

Good idea! +1

neograph734’s picture

StatusFileSize
new10.47 KB
new1.22 KB

Switching to the cleaner array_column.

Edit:
Ah, array_column does not preserve the array keys so the select does not work as expected....

neograph734’s picture

StatusFileSize
new10.71 KB
new1.18 KB

I was hoping for a more elegant method, but this same solution is used in several more places in core, so I think it is good enough.

Please let me know what you think.

The patch from #52 should still appy against Drupal 9.

andypost’s picture

It looks great 👍

@Neograph734 I'll check tests tomorrow but looks everything is covered.

Few nitpicks but let's get review from maintainers

andypost’s picture

I'm curious if we should check parsed yml more

- when scheme missing color, should we use default one instead? I mean inheritance of colors in scheme/palette
- should the parser validate colors for correct values?
- how this could be extended to allow configurable colors (probably color module 2.x)

andypost’s picture

StatusFileSize
new3.69 KB
new10.69 KB

Added some clean-up

neograph734’s picture

Thanks for the feedback and the small update!

- When scheme missing color, should we use default one instead? I mean inheritance of colors in scheme/palette
- Should the parser validate colors for correct values?

It depends on the scope of this issue. I have constantly considered this a mechanism for a theme builder to expose a theme's CSS variables and optionally ship some nice combinations (color schemes). There is some assumption from me that a theme builder knows what he is doing and therefore we can keep this parser as lightweight as possible.

How this could be extended to allow configurable colors (probably color module 2.x)

Modern CSS allows the usage of variables like this:

:root {
  --color-base-primary-color: yellow;
}

body {
  background-color: var(--color-base-primary-color);
}

A theme buider can use this to provide default variable values, but they can be overridden by also defining these variables in the style tag of an HTML element:

/**
 * Implements hook_preprocess_HOOK() for html renderings.
 */
function HOOK_preprocess_html(&$variables) {
  $variables['html_attributes']->setAttribute('style', '--color-base-primary-color: red');
}

What is currently missing is that there is no way for Drupal to know which color variables a theme uses. The parser in this patch attempts to fill this gap by making the used color variables available to Drupal by interpreting a THEME.colors.yml file:

colors:
  base_primary_color: 'Primary base color'

schemes:
  default:
    label: 'Blue Lagoon'
    colors:
      base_primary_color: '#1b9ae4'

All that is now needed is to make HOOK_preprocess_html() a little more dynamic and to provide a form to fill in some other color values. Which has already been done in Olivero, but also provides a base for a color2 module.

andy-blum’s picture

I haven't done an in-depth review of the most recent patch, but there are a couple of things I'm noticing that I'd suggest changes on:

1. We're looping over the color field keys to generate the color inputs

foreach ($color_config['colors'] as $key => $title) {
    $form['olivero_settings']['olivero_utilities']['olivero_color_scheme'][$key] = [
      ...
    ];
  }

But we're hard-coding base_primary_field in the form validation function. We'll need to make sure that validation function applies to all the colors defined in .colors.yml

2. This implementation requires themers to effectively duplicate the code out of Olivero to utilize this functionality. Could all the color field additions happen somewhere else so theme creators only need to implement theme_name.colors.yml to get this functionality in their themes?

neograph734’s picture

Thanks for the reply Andy. Again, it depends a bit on how far we want to take this.

We've had some discussion in #2808151-59: [policy] Move the Color module to a contributed project when Bartik is deprecated, but color is just deprecated and Olivero made it to core, so it cannot rely on contrib modules. This leaves Olivero in a bit of an awkward situation with its own implementation of this.
That also means that I see hard coding not as a big issue in this situation (though it's not pretty).

If you could have a look at the color POC sandbox module I already mentioned several times above, all that is needed is a yml file and obviously correctly named variables. So point 2 is fully covered. But in a contrib module.

Update
For now I have not scoped this to be a theme color changing API, but rather (at least) have a universal way for theme builders to expose their changeable colors before every theme builders builds their own method. Perhaps we can improve on this later and shape it more into an API later.

andy-blum’s picture

I know we just deprecated color out of core, but I do think having something like this (with additional features) in core has an incredible amount of value. I don't think the color modules purpose yielded its deprecation, just its execution. This issue and Olivero's DIY solution are evidence of that, I believe.

I think if we update the validation function in this patch to dynamically apply as well, I see no reason this shouldn't be ready for core. Should we open some follow-up issues to expand on this functionality? Is there some core maintainer that needs to sign off on this?

neograph734’s picture

Assigned: Unassigned » neograph734

I believe recolorable themes have a right to exist too. But I am a bit hesistant to turn this into a full-function API, so I am wondering what additional features you are referring to.

This patch provides a minimal base for theme builders to have one generic way of exposing color variables. It is the minimal needed for any contrib module to be universally applicable to all themes, thus the most minimal for color in contrib to have a chance.

And there is not even that much to expand on. Inserting CSS variables back into the page is very trivial, but depends on the place where they are stored. I think a theme's third party settings make sense, but in Olivero's case it is in the regular theme settings. This would break the universal applicability.
Most of what is left would be forms, and adding those would make this effectively adding color back into core.

I hope a maintainer will chime in soon so we can get some guidance.

We can make Olivero's validation function dynamic, I think that you are right and that it would complete the integration. Self assignign to finish that.

neograph734’s picture

Assigned: neograph734 » Unassigned
StatusFileSize
new11.85 KB
new1.98 KB

Updated the validation to be dynamic.

andy-blum’s picture

The primary things I'd suggest as a followup issues, in no particular order:

  • Add a php class for backend color conversions
  • Add a js library for frontend color conversions
  • Add some new schema types to correctly store hsl(a)/rgb(a) values in the database so we don't have to do a color conversion on every page load
andypost’s picture

The roadmap #70 sounds great!

bbrala made their first commit to this issue’s fork.

bbrala’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

Let me preface this with; I don't theme ;) But I had a look anyways.

This is a great way to make colors more extensible! I've went through the code and have some thought on how we can improve.

  1. +++ b/core/lib/Drupal/Core/Theme/ThemeColorsParser.php
    @@ -0,0 +1,72 @@
    + * Parses an optional theme .colors.yml file.
    

    It seems to be customary to refer to files like this as THEME.colors.yml

  2. +++ b/core/lib/Drupal/Core/Theme/ThemeColorsParser.php
    @@ -0,0 +1,72 @@
    +   * Information stored in a theme .colors.yml file:
    

    and here

  3. +++ b/core/lib/Drupal/Core/Theme/ThemeColorsParser.php
    @@ -0,0 +1,72 @@
    +   * See olivero.colors.yml for an example of a theme .colors.yml file.
    

    and here

  4. +++ b/core/lib/Drupal/Core/Theme/ThemeColorsParser.php
    @@ -0,0 +1,72 @@
    +  public function parse(string $filename): array {
    +    $defaults = [
    +      'colors' => [],
    +      'schemes' => [],
    +    ];
    +
    +    if (!file_exists($filename)) {
    +      return $defaults;
    +    }
    +    try {
    +      $parsed_info = Yaml::decode(file_get_contents($filename)) + $defaults;
    +    }
    +    catch (InvalidDataTypeException $e) {
    +      throw new \RuntimeException("Unable to parse $filename " . $e->getMessage());
    +    }
    

    This part might be a little scary. I mean, the Yaml library is probably pretty safe, and would probably break if something is not yaml. But in core we do tend to be explicit in the fact the file is a specific .yml file. At some point in the pipeline before parsing we always know its an yml, and even what kind of yml (.KIND.yml).

    I think we need to make sure we are at least reading a YML file so some sort of check is needed in my opinion. Even if is just a check for .colors.yml.

    A little voice in my head says to me that perhaps this should be even more coupled to themes, where you just pass the theme name perhaps and uses something like \Drupal\Core\Extension\ThemeExtensionList to find the correct path and only parse THEME.colors.yml. This might be a little convuluted though.

I think we do need someone from the theme subsystem to review this also. There is other things we can do while we wait for that though, there will be need for a change record and a release note snippet. Would this also need to be documented in theme.api.php? Not sure about that.

Subsystem maintainers for Theme api:
- Alex Bronstein 'effulgentsia' https://www.drupal.org/u/effulgentsia
- Fabian Franz 'Fabianx' https://www.drupal.org/u/fabianx
- Joël Pittet 'joelpittet' https://www.drupal.org/u/joelpittet
- Lauri Eskola 'lauriii' https://www.drupal.org/u/lauriii

Note: i think i pushed create issue fork by accident while reviewing? Sorry.

Ratan Priya’s picture

StatusFileSize
new11.86 KB
new1.2 KB

@bbrala,
Worked on points 1,2,3 as per comment #73. Still needs work for point 4.
Needs review.

neograph734’s picture

Regarding point 4, I think everything in core uses that same class in one way or another.

I can agree that we could try to make it a bit more pretty by using the YamlDiscoveryDecorator, but if you dig deep enough you end up with the same Yaml class.

We would then more or less go the route that the layout discovery module takes for detecting theme.layout.yml files, but since color was just removed from core I did not want to introduce another module to substitute only part of it.
For now I've focussed on a light-weight solution. At least until we have some subsystem manager's feedback. (Note that such decorator could be part of a color2 module, but for reasons mentioned earlier, I think it is better to get some generic implementation into core now.)

bbrala’s picture

As mentioned in my comment, the people who are subsystem maintainer for theming don't include me, I'm from the flipside, jsonapi.

Regarding #4 mostly my point is we need a little more security regarding what we feed the parser. I think a check on the file pattern could be good enough.

andy-blum’s picture

Version: 10.0.x-dev » 10.1.x-dev
Status: Needs work » Needs review
StatusFileSize
new11.96 KB
new570 bytes

Per @bbrala in #73:

At some point in the pipeline before parsing we always know its an yml, and even what kind of yml (.KIND.yml).

I think we need to make sure we are at least reading a YML file so some sort of check is needed in my opinion. Even if is just a check for .colors.yml.

Updated patch & interdiff addressing this point attached.

andy-blum’s picture

Issue summary: View changes

Updating IS.

andy-blum’s picture

Issue summary: View changes
neograph734’s picture

Issue summary: View changes

andy-blum’s picture

StatusFileSize
new104.61 KB

MR opened in #81 is still a work in progress, but moves the form element creation into core/modules/system/src/Form/ThemeSettingsForm.php. Moving the logic away from individual themes like this allows us to add just the THEMENAME.colors.yml file to a theme and have all the forms appear automatically. For example, create a claro.colors.yml file and add the code below:

colors:
  primary_color: 'Primary color'
  accent_color: 'Accent color'

schemes:
  default:
    label: 'Admin Blue'
    colors:
      primary_color: '#1b9ae4'
      accent_color: '#0000ff'
  admin_red:
    label: 'Admin Red'
    colors:
      primary_color: '#a30f0f'
      accent_color: '#ff0000'
  admin_purple:
    label: 'Admin Purple'
    colors:
      primary_color: '#7a4587'
      accent_color: '#ff00ff'

This should add a new section to the Claro settings with no additional changes.

new color fields exist in the claro settings form relating to the code provided above

andy-blum’s picture

Status: Needs review » Needs work
andy-blum’s picture

Issue summary: View changes
andy-blum’s picture

I had anticipated the MR failing some Olivero tests at this point, but that doesn't appear to be happening.

#3257274: Implement color changing theme settings for Olivero added 4 tests:

  1. core/tests/Drupal/Nightwatch/Tests/Olivero/oliveroColorTest.js
  2. core/themes/olivero/tests/fixtures/update/olivero-3257274.php
  3. core/themes/olivero/tests/src/Functional/Update/OliveroPostUpdateTest.php
  4. core/themes/olivero/tests/src/Unit/OliveroHexToHslTest.php

I'm not sure if any of these need modified. OliveroColorTest.js is the one that covers the MR the most, and while it's not Olivero-specific code now, Olivero is the only place using it right now.

Moving to needs review to get eyes on this at this stage.

andy-blum’s picture

Status: Needs work » Needs review
andy-blum’s picture

During a call this morning with @bnjnmn, @mherchel, @ckrina, and @andypost, we discussed the next steps for this issue:

  1. Do a dependency eval for spatie/color. The PHP side was determined to be the more important need and likely an easier win than also adding a JS runtime dependency.
  2. Adding in new schema types for HSL(a) and RGB(a) at a minimum. These are both widely supported & used, and are a minimal addition to the core data types. Other color schemas could be added in the future if their addition makes sense.

#f052bfdc begins work on both of those points.

Below is the beginning of the dependency eval process for spatie/color:

1. Maintainership of the package

Color is maintained by a Belgian Laravel agency, Spatie. The company has a strong history of open source. The package was created in fall of 2016, and was last updated December 2022. There are three open issues in the issue queue, all enhancements, from 2017. This package has ~830k downloads on packagist and ~280 stars on github.

2. Security policies of the package

The project's README states, "If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker."

3. Expected release and support cycles

Project releases do not appear to have a regular cadence, but the need served by the project is fairly small and unchanging. Releases are outlined on the project's github page, and there were multiple releases in each of 2020, 2021, and 20222.

4. Code quality

I do not know how to evaluate this, but I didn't have any issues reading the code and understanding what was available.

5. Other dependencies it would add, if any (the full tree, not just direct dependencies), and evaluations for those dependencies as well

spatie/color only lists two development dependencies for testing purposes, "pestphp/pest" and "phpunit/phpunit".

andy-blum’s picture

Adding tags for framework manager and release manager review per dependency evaluation docs

andypost’s picture

VladimirAus made their first commit to this issue’s fork.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new5 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

vladimiraus’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new5.27 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Sana.Neyazi’s picture

Hi All, As we know this "color" module will be removed in drupa10 and and we can use this module of its contributed version.
Can anyone please help me what is the best way to switching color module from core to contrib version.
Below steps I followed but not sure anything more needed here.

1. Took backup of my db
2. Commented out the dependency of this module (we are using color module in our custom theme)
3. Uninstall core color module
4. Download contributed COLOR module using composer
composer require 'drupal/color:^1.0'
5. Now COLOR module is not showing under core

Can anyone please suggest me is there any more steps need to do here or is it fine?

Thanks in advance

andypost’s picture

@Sana.Neyazi you just need to install contrib module via composer, see https://www.drupal.org/docs/core-modules-and-themes/deprecated-and-obsolete

The MR still needs work for 11.x branch

andy-blum’s picture

Issue summary: View changes

Dependency evaluation added to issue summary.

catch’s picture

Checked composer.json of spatie/color and it has "php" : "^7.3|^8.0" which is a good sign. Either adopting PHP versions too late, or dropping them too early, has been one of our bigger issues with PHP dependencies.

To me this looks like a low-risk dependency addition.

edit: for code quality I usually look at:

1. Is it modern PHP (i.e. not some weird 15 year old PEAR library)
2. Does it have test coverage
3. Is it readable / well documented

For something like this, we're using it as a utility library, and not particularly exposing the API via Drupal (unlike Guzzle, or Symfony routing etc.) and it doesn't have a big security surface area (like Twig) so as long as it makes sense to itself then it's just so someone's actually looked at the code a bit before we bring it in.

bbrala’s picture

The guys from spatie are well know for their open source packages. It's a belgian agency and extremely well known entity in the laravel ecosystem

longwave’s picture

Agree with everything @catch said in #98. I checked the release history and they were quick to add support for PHP 8 and then 8.2. The code is fairly simple - largely just a bunch of calculations to convert colour values of one type to another - and it has unit tests. There are almost no code comments, which is one minor downside - but then again it is just calculations and there are no algorithms as such to describe.

I don't expect there to be any security issues nor any large scale rewrites given that it is a narrowly focused utility library. In the unlikely case of abandonment we could always adopt the code that we are using and bring it into core if needed given there are only a handful of classes (and we are only likely to use a subset of those, even).

Removing the tag given that two RMs have reviewed this and come to the same conclusion.

I haven't looked too closely but we could probably deprecate \Drupal\Component\Utility\Color after bringing this in.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.