Skip to content

Instantly share code, notes, and snippets.

@andrewrcollins
Last active February 24, 2024 18:52
Show Gist options
  • Select an option

  • Save andrewrcollins/4570993 to your computer and use it in GitHub Desktop.

Select an option

Save andrewrcollins/4570993 to your computer and use it in GitHub Desktop.
Color Mixing, Tint, Tone, and Shade in PHP
<?php
/**
* mix
*
* @param mixed $color_1
* @param mixed $color_2
* @param mixed $weight
*
* @return void
*/
function mix($color_1 = array(0, 0, 0), $color_2 = array(0, 0, 0), $weight = 0.5)
{
$f = function ($x) use ($weight) {
return $weight * $x;
};
$g = function ($x) use ($weight) {
return (1 - $weight) * $x;
};
$h = function ($x, $y) {
return round($x + $y);
};
return array_map($h, array_map($f, $color_1), array_map($g, $color_2));
}
/**
* tint
*
* @param mixed $color
* @param mixed $weight
*
* @return void
*/
function tint($color, $weight = 0.5)
{
$t = $color;
if (is_string($color)) {
$t = hex2rgb($color);
}
$u = mix($t, array(255, 255, 255), $weight);
if (is_string($color)) {
return rgb2hex($u);
}
return $u;
}
/**
* tone
*
* @param mixed $color
* @param mixed $weight
*
* @return void
*/
function tone($color, $weight = 0.5)
{
$t = $color;
if (is_string($color)) {
$t = hex2rgb($color);
}
$u = mix($t, array(128, 128, 128), $weight);
if (is_string($color)) {
return rgb2hex($u);
}
return $u;
}
/**
* shade
*
* @param mixed $color
* @param mixed $weight
*
* @return void
*/
function shade($color, $weight = 0.5)
{
$t = $color;
if (is_string($color)) {
$t = hex2rgb($color);
}
$u = mix($t, array(0, 0, 0), $weight);
if (is_string($color)) {
return rgb2hex($u);
}
return $u;
}
/**
* hex2rgb
*
* @param mixed $hex
*
* @return void
*/
function hex2rgb($hex = '#000000')
{
$f = function ($x) {
return hexdec($x);
};
return array_map($f, str_split(str_replace("#", "", $hex), 2));
}
/**
* rgb2hex
*
* @param mixed $rgb
*
* @return void
*/
function rgb2hex($rgb = array(0, 0, 0))
{
$f = function ($x) {
return str_pad(dechex($x), 2, "0", STR_PAD_LEFT);
};
return "#" . implode("", array_map($f, $rgb));
}
<?php
include __DIR__ . DIRECTORY_SEPARATOR . "mix_tint_tone_shade.php";
// base color
$base_color = "#67ff34";
print_r($base_color);
echo "\n";
// -----
// test hex2rgb
print_r(hex2rgb($base_color));
echo "\n";
// -----
// test rgb2hex
print_r(rgb2hex(hex2rgb($base_color)));
echo "\n";
// -----
// test tint (hex)
print_r(tint($base_color));
echo "\n";
// test tint (rgb)
print_r(tint(hex2rgb($base_color)));
echo "\n";
// -----
// test tone (hex)
print_r(tone($base_color));
echo "\n";
// test tone (rgb)
print_r(tone(hex2rgb($base_color)));
echo "\n";
// -----
// test shade (hex)
print_r(shade($base_color));
echo "\n";
// test shade (rgb)
print_r(shade(hex2rgb($base_color)));
echo "\n";
// -----
@markoliverbrawn

Copy link
Copy Markdown

Excellent. Simple and clean and uncluttered. Nice.

@andrewrcollins

Copy link
Copy Markdown
Author

Thanks!

@iLenTheme

Copy link
Copy Markdown

Thank you very much, that was looking for a long time

@thegroovetrain

Copy link
Copy Markdown

This was really handy for me in developing an automatic palette generator. I made a couple of modifications to your code, such as reversing the $f and $g anonymous functions of mix so that, for example if you're shading, the weight is the amount of shade (eg 0.2 is 20% shade), as well as condensing $t declarations and return lines in the shade, tone and tint functions into ternary operators. I made a fork in case you're interested. Thanks so much!

@877361

877361 commented Jan 24, 2024

Copy link
Copy Markdown

91 club hack

@877361

877361 commented Jan 24, 2024

Copy link
Copy Markdown

91 club hack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment