I was curious to know how to incorporate CSS SWATCHES into my web pages. The idea is to set the CSS Variables to CSS SWATCHES and use the CSS Var variables in the CSS file.
I knocked up the following and set the CSS SWATCH Colours like the following:
:root {
--gff: RED;
--dad: ORANGE;
--mum: PINK;
--son: LIGHTPINK;
--bby: ROSE;
}
Variable are abreviations of grandfather, father mother, son and baby which no doubt could be improved
PHP Source flle that generates the web pages:
<?php declare(strict_types=1);
$title = 'JB Testing CSS Variables';
$swatch = getSwatch(); // function below
$heading = getHeading($title, $swatch); // function below
$source = highlight_file(__FILE__, TRUE);
# RENDER STARTS HERE >>>>>>>>>>>>>>>>
echo $header = <<< ____EOT
$heading
<body>
<header>
<h1> $title </h1>
<nav>
<a href="?"> Default </a>
<a href="?swatch=red"> Red </a>
<a href="?swatch=blue"> Blue </a>
<a href="?swatch=green"> Green </a>
</nav>
</header>
____EOT;
echo $MAIN = <<< ____EOT
<main>
<section>
<h2> Just Testing Folks </h2>
<p>
<p> Goes here </p>
</p>
<dl>
<dt>
<dl> Goes here </dl>
</dt>
<dd>
<dd> Goes here </dd>
</dd>
</dl>
<div>
<div> Goes here </div>
</div>
<pre>
<pre> Goes here </pre>
</pre>
</section>
<section>
<h2> Source: </h2>
<div>
$source
</div>
</section>
</main>
<footer>
<footer> Goes here </footer>
</footer>
____EOT;
require'/var/www/footer-side.php';
echo'</body></html>';
# RENDER ENDS HERE >>>>>>>>>>>>>>>>>>>>>>>
// ONLY FUNCTIONS BELOW
# ======================================
function getHeading(string $title, string $swatch)
:string
{
$result = <<< ______TMP
<!DOCTYPE HTML><html lang = 'en-GB'>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style> $swatch </style>
<link type="text/css" href="style.css" media="screen" rel="stylesheet">
<link rel="shortcut icon" href="./favicon.ico">
<title> $title </title>
</head>
______TMP;
return $result;
}//
# ======================================
function getSwatch()
:string
{
$clrs = isset($_GET['swatch']) ? $_GET['swatch'] : 'DEFAULT';
switch( strtoupper($clrs) ):
case "RED" :
$swatch = <<< ____EOT
:root {
--gff: RED;
--dad: ORANGE;
--mum: PINK;
--son: LIGHTPINK;
--bby: ROSE;
}
____EOT;
break;
case "BLUE" :
$swatch = <<< ____EOT
:root {
--gff: BLUE;
--dad: LIGHTBLUE;
--mum: AQUA;
--son: AQUA;
--bby: SKYBLUE;
}
____EOT;
break;
case "GREEN" :
$swatch = <<< ____EOT
:root {
--gff: GREEN;
--dad: LIMEGREEN;
--mum: LIGHTGREEN;
--son: PALEGREEN;
--bby: LAWNGREEN;
}
____EOT;
break;
default:
$swatch = <<< ____EOT
:root {
--gff: GRAY;
--dad: SLATEGRAY;
--mum: LIGHTGRAY;
--son: DARKGRAY;
--bby: SLATEGRAY;
}
____EOT;
endswitch; // ($clrs):
return $swatch;
}//
I’m curious to know if this could be improve with JavaScript.
Edit:
Renamed and modified original file and added source,php instead of complicatiing the original web page.