I’d like to specify 10 colors the client can use to color the heading on different pages, and to have different background images (so a blue color might have a predominely blue background image). So that the heading and background image are different on different pages.
Can I get the Color field to add a class name to the <body> so that I can select it in the CSS and style the heading and background image?
it would be nice to use the Color field so the client has a visual guide (rather than a select field)
So if the user in the Panel selected Color A then there is a way to add this as a class to an element? For example:
<body class="colorA">
Does the Color field store raw color values (rgb, # etc) in the content files? If so, I need a way to convert the color value into a css class name. Is this what the ‘options array’ in the config does? If so, this is well outside my comfort zone. Think I’ll use a select field instead.
Out of interest, if the Color field stores raw color values, how do you use that in a template? Using style?
Ok… this is probably a dim question, but how do I use this in the template? The ‘lookup table’ converts the stored color value to a label (that could be used as a class name) – but I don’t understand how to add this class to an element? Is it as simple as:
$colors = option('my.colors'); // returns the color definition array from your config
// Build lookup table (you would do this only once somewhere
$colorMap = array_column(
$colors,
'class',
'hex'
);
// normalize value
$hex = strtolower($page->color()->value()); // say your color value is #3e3e3e
$class = $colorMap[$hex] ?? null;
echo $class; // result: dull