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
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.- 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.
- 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:
-
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.
-
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."
-
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.
-
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.
-
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.
| Comment | File | Size | Author |
|---|
Issue fork drupal-788332
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:
- 788332-provide-a-parser
compare
- 788332-theme-colors-yml
changes, plain diff MR !3620
Comments
Comment #1
tim.plunkettLikely not backportable, but interesting idea.
Comment #2
markhalliwellThis 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".
Comment #3
markhalliwellThis patch needs a reroll/refactor.
Comment #4
markhalliwellWe're past code freeze, postponing to 9.x.
Comment #5
markhalliwellWas talking with alexpott about this and this seems to make the most sense. We could leverage the new config entities for this as well.
Comment #6
catchThis could be done with bc in a minor release.
Comment #7
tim.plunkettNote that #2808151: [policy] Move the Color module to a contributed project when Bartik is deprecated is being discussed as well.
Comment #9
anish.a commentedI 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.
Comment #10
anish.a commentedI was mistaken. The color.inc is in individual themes (like bartic)
Comment #11
jofitzJust to get this off the "Needs Reroll" list!
Comment #12
Jeff Burnz commentedSure, as long as it's BC, all good. I have a lot of colorable themes in the wild.
Comment #15
borisson_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?
Let's just remove this line if we no longer need it, having it in comment doesn't seem like a great plan.
What does this do? Should we not remove this?
#5 mentions that this should be in a theme.color.yml file, this is looking for a .info.yml file.
Which one is correct?
Let's invert this if to make the code easier to grok.
If we need this additional validation, let's move it to some kind of utility class to make testing this possible.
Comment #17
andypostTo 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.ymlThe single fine is easy to discover with core's
\Drupal\Core\Plugin\Discovery\YamlDiscoveryComment #18
YurkinPark commentedI'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
Possibly this is another task, but i'm sure generating of this file can be used by some option inside .color.yml file
Comment #20
andypost@YurkinPark looks you made patch not from core's git repo
Comment #21
YurkinPark commentedSeems like yes, i've selected wrong directory during patch generating, attaching new one and interdiff for patch #11
Comment #23
YurkinPark commentedand again, sorry
Comment #24
YurkinPark commentedComment #28
andypostComment #29
xjmComment #33
neograph734Updating 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.
Comment #34
neograph734So 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:
Result:
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\Themenamespace 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?
Comment #35
neograph734Updating the name and moving this from color module to the theme system.
Comment #36
andypostIt should be easier to create 2.x branch in dgo.to/color and work there
Comment #37
neograph734@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.
Comment #38
ranjith_kumar_k_u commentedComment #40
neograph734That was an unrelated fail. Setting back to NR. Thanks @ranjith_kumar_k_u for fixing the coding standard.
Comment #41
andypostLooks great! Also colors has different
#fffvshsl()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
I prefer palette wording
Comment #42
neograph734Thanks for the feedback andypost! Sharing my thoughts below.
Do we? An HTML color element only accepts a HEX value (documentation) and in
core.data_types.schema.ymlthere 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.
I am not that familiar with all the namespaces provided by Drupal, so I would need some guidance for a better place then.
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:
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.
Comment #43
andy-blum@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:
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?
Comment #44
andy-blumAlso, 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?
Comment #45
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.
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.
Comment #46
andy-blum@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
Comment #47
saschaeggiExcited to also use this in Gin soon! 🙏👏
Comment #48
andy-blum@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
Comment #49
neograph734I 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.
Comment #50
neograph734Well, that's some really strict checking... Omitting the interdiff due to the minimalism of the changes.
Comment #52
neograph734This should make them all pass.
Comment #54
neograph734Heh, my test passes, but it is the same test that caused #38 to fail the first time. Retesting.
Comment #55
neograph734Updating 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.
Comment #56
andypostI bet that's better/faster place to collaborate
Comment #57
neograph734@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!
Comment #58
andypostGood idea! +1
Comment #59
neograph734Switching to the cleaner array_column.
Edit:
Ah, array_column does not preserve the array keys so the select does not work as expected....
Comment #60
neograph734I 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.
Comment #61
andypostIt looks great 👍
@Neograph734 I'll check tests tomorrow but looks everything is covered.
Few nitpicks but let's get review from maintainers
Comment #62
andypostI'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)
Comment #63
andypostAdded some clean-up
Comment #64
neograph734Thanks for the feedback and the small update!
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.
Modern CSS allows the usage of variables like this:
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:
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:
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.Comment #65
andy-blumI 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
But we're hard-coding
base_primary_fieldin the form validation function. We'll need to make sure that validation function applies to all the colors defined in .colors.yml2. 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?
Comment #66
neograph734Thanks 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.
Comment #67
andy-blumI 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?
Comment #68
neograph734I 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.
Comment #69
neograph734Updated the validation to be dynamic.
Comment #70
andy-blumThe primary things I'd suggest as a followup issues, in no particular order:
Comment #71
andypostThe roadmap #70 sounds great!
Comment #73
bbralaLet 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.
It seems to be customary to refer to files like this as THEME.colors.yml
and here
and here
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\ThemeExtensionListto 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.
Comment #74
Ratan Priya commented@bbrala,
Worked on points 1,2,3 as per comment #73. Still needs work for point 4.
Needs review.
Comment #75
neograph734Regarding 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.)
Comment #76
bbralaAs 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.
Comment #77
andy-blumPer @bbrala in #73:
Updated patch & interdiff addressing this point attached.
Comment #78
andy-blumUpdating IS.
Comment #79
andy-blumComment #80
neograph734Comment #82
andy-blumMR 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:
This should add a new section to the Claro settings with no additional changes.
Comment #83
andy-blumComment #84
andy-blumComment #85
andy-blumI 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:
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.
Comment #86
andy-blumComment #87
andy-blumDuring a call this morning with @bnjnmn, @mherchel, @ckrina, and @andypost, we discussed the next steps for this issue:
#f052bfdc begins work on both of those points.
Below is the beginning of the dependency eval process for spatie/color:
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.
The project's README states, "If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker."
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.
I do not know how to evaluate this, but I didn't have any issues reading the code and understanding what was available.
spatie/color only lists two development dependencies for testing purposes, "pestphp/pest" and "phpunit/phpunit".
Comment #88
andy-blumAdding tags for framework manager and release manager review per dependency evaluation docs
Comment #89
andypostFYI OpenSocial depends on spatie/color https://github.com/goalgorilla/open_social/blob/e5656027a3f3e0000cb97625...
Comment #91
needs-review-queue-bot commentedThe 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.
Comment #92
vladimirausComment #93
needs-review-queue-bot commentedThe 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.
Comment #95
Sana.Neyazi commentedHi 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
Comment #96
andypost@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
Comment #97
andy-blumDependency evaluation added to issue summary.
Comment #98
catchChecked 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.
Comment #99
bbralaThe 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
Comment #100
longwaveAgree 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\Colorafter bringing this in.