/*
Color lib by Yosko (http://www.yosko.net/)
Licence: WTFPL except for the following
source for HSL algorithm: http://www.easyrgb.com/index.php?X=MATH
*/
//returns a clean 6 digit hex number as a string
function cleanHexColor($hex) {
$hex = strtolower($hex);
//remove the leading "#"
if(strlen($hex) == 7 || strlen($hex) == 4)
$hex = substr($hex, -(strlen($hex)-1));
// $hex like "1a7"
if(preg_match('/^[a-f0-9]{6}$/i', $hex))
return $hex;
// $hex like "162a7b"
elseif(preg_match('/^[a-f0-9]{3}$/i', $hex))
return $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
//any other format
else
return "000000";
}
//returns an array of 3 [0-255] int
function hex2rgb($hex) {
return array(
hexdec(substr($hex, 0, 2)),
hexdec(substr($hex, 2, 2)),
hexdec(substr($hex, 4, 2)),
);
}
//returns a 6 digit hex number as a string
function rgb2hex($rgb) {
return ""
.substr("00".dechex(ceil($rgb[0])), -2)
.substr("00".dechex(ceil($rgb[1])), -2)
.substr("00".dechex(ceil($rgb[2])), -2)
;
}
//returns a 2 dimensional array of subarrays (HSL: 3 values [0-1] floats)
function variationAround($hsl, $stepSize = 0.2, $constant = "hue") {
if($constant == "hue") {
$i = 1; $j = 2;
} elseif($constant == "sat") {
$i = 0; $j = 2;
}if($constant == "lum") {
$i = 0; $j = 1;
}
$variations = array(); //resulting variations
$hslTemp = array(); //stores current variation
$mod = array(); //stores modulos for h, s & l
$mod[0] = (float)(($hsl[0]*100) % ($stepSize * 100)) / 100;
$mod[1] = (float)(($hsl[1]*100) % ($stepSize * 100)) / 100;
$mod[2] = (float)(($hsl[2]*100) % ($stepSize * 100)) / 100;
$hslTemp = $hsl;
$hslTemp[$i] = $mod[$i];
while($hslTemp[$i] <= 1) {
$row = array();
$hslTemp[$j] = $mod[$j];
while($hslTemp[$j] <= 1) {
$row[(string)$hslTemp[$j]] = array($hslTemp[0], $hslTemp[1], $hslTemp[2]);
$hslTemp[$j] = (float)($hslTemp[$j] + $stepSize);
}
$variations[(string)$hslTemp[$i]] = $row;
$hslTemp[$i] = (float)($hslTemp[$i] + $stepSize);
}
return $variations;
}
function rgb2hsl($rgb) {
//work with range [0-1] for RGB values
$r = $rgb[0] / 255;
$g = $rgb[1] / 255;
$b = $rgb[2] / 255;
//get min and max from RGB
$min = min($r, $g, $b);
$max = max($r, $g, $b);
$delta = $max-$min;
//luminance
$l = ($max + $min) / 2;
//saturation & hue
if($min == $max) {
//grayscale
$s = 0;
$h = 0;
} else {
//saturation
if($l < 0.5) $s = $delta/($max+$min);
else $s = $delta/(2-$max-$min);
//hue
if($r == $max) $h = ($g-$b)/(6*$delta);
elseif($g == $max) $h = 1/3 + ($b-$r)/(6*$delta);
elseif($b == $max) $h = 2/3 + ($r-$g)/(6*$delta);
//keep hue in the range [0,1]
if ($h < 0) $h++;
if ($h > 1) $h--;
}
return array($h, $s, $l);
}
function hsl2rgb($hsl) {
$h = $hsl[0];
$s = $hsl[1];
$l = $hsl[2];
//no saturation: grayscale
if($s == 0) {
$r = $g = $b = round($l * 255);
} else {
if($l < 0.5) $var2 = $l * (1 + $s);
else $var2 = ($l + $s) - ($s * $l);
$var1 = 2 * $l - $var2;
$r = 255 * hue2rgb($var1, $var2, $h + 1/3);
$g = 255 * hue2rgb($var1, $var2, $h);
$b = 255 * hue2rgb($var1, $var2, $h - 1/3);
}
return array($r, $g, $b);
}
function hue2rgb($v1, $v2, $vH)
{
if ( $vH < 0 ) $vH += 1;
if ( $vH > 1 ) $vH -= 1;
if ( $vH < 1/6 ) return $v1 + ( $v2 - $v1 ) * 6 * $vH;
elseif ( $vH < 1/2 ) return $v2;
elseif ( $vH < 2/3 ) return $v1 + ( $v2 - $v1 ) * ( 2/3 - $vH ) * 6;
else return ( $v1 );
}
PHP color
<iframe width="100%" height="2774" src="http://snip.yosko.net/index.php?embed=5200a8226193a" type="text/html"></iframe>
Text only - Permalink - Snippet public post date 06/08/2013
private void RotateColors(Bitmap image, float degrees)
{
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;
double r = degrees * System.Math.PI / 180; // degrees to radians
float cosR = (float)Math.Cos(r);
float sinR = (float)Math.Sin(r);
float a = (1 + 2 * cosR) / 3;
float b = ((1 - cosR) - (float)Math.Sqrt(3) * sinR) / 3;
float c = ((1 - cosR) + (float)Math.Sqrt(3) * sinR) / 3;
float[][] colorMatrixElements = {
new float[] {a, b, c, 0, 0},
new float[] {c, a, b, 0, 0},
new float[] {b, c, a, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
using (Graphics g = Graphics.FromImage(image))
{
g.DrawImage(
image,
new Rectangle(0, 0, width, height), // destination rectangle
0, 0, // upper-left corner of source rectangle
width, // width of source rectangle
height, // height of source rectangle
GraphicsUnit.Pixel,
imageAttributes);
}
}
CSharp color
<iframe width="100%" height="866" src="http://snip.yosko.net/index.php?embed=5200a7a453a92" type="text/html"></iframe>
Text only - Permalink - Snippet public post date 06/08/2013