| 1 |
<?php
|
| 2 |
// $Id: shift.inc,v 1.1.2.1 2008/08/20 16:45:41 skiquel Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Shift engine
|
| 6 |
* Shifts colors based on 'base' color
|
| 7 |
*/
|
| 8 |
function _color_engine_shift($style, $info, $palette, $options) {
|
| 9 |
$link = (array_search('link', $options) !== FALSE) ? TRUE : FALSE;
|
| 10 |
$text = (array_search('text', $options) !== FALSE) ? TRUE : FALSE;
|
| 11 |
|
| 12 |
// Prepare color conversion table
|
| 13 |
$conversion = $palette;
|
| 14 |
|
| 15 |
foreach ($conversion as $k => $v) {
|
| 16 |
$conversion[$k] = drupal_strtolower($v);
|
| 17 |
}
|
| 18 |
// Original spot is right here, I'm moving up so i can see it in the debug
|
| 19 |
// Bind reference hex to fields
|
| 20 |
$reference = array_combine($info['fields'], $info['reference_scheme']);
|
| 21 |
|
| 22 |
// Find all colors in the stylesheet and the chunks in between.
|
| 23 |
$style = preg_split('/(#[0-9a-f]{6}|#[0-9a-f]{3})/i', $style, -1, PREG_SPLIT_DELIM_CAPTURE);
|
| 24 |
$is_color = FALSE;
|
| 25 |
$output = '';
|
| 26 |
$base = 'base';
|
| 27 |
foreach ($style as $chunk) {
|
| 28 |
if ($is_color) {
|
| 29 |
$chunk = drupal_strtolower($chunk);
|
| 30 |
// Check if this is one of the colors in the default palette.
|
| 31 |
if ($key = array_search($chunk, $reference)) {
|
| 32 |
$chunk = $conversion[$key];
|
| 33 |
}
|
| 34 |
// Not a pre-set color. Extrapolate from the base.
|
| 35 |
else {
|
| 36 |
$chunk = _color_shift($palette[$base], $reference[$base], $chunk, $info['blend_target']);
|
| 37 |
}
|
| 38 |
}
|
| 39 |
else {
|
| 40 |
// Determine the most suitable base color for the next color.
|
| 41 |
|
| 42 |
// 'a' declarations. Use link.
|
| 43 |
if ($link && preg_match('@[^a-z0-9_-](a)[^a-z0-9_-][^/{]*{[^{]+$@i', $chunk)) {
|
| 44 |
$base = 'link';
|
| 45 |
}
|
| 46 |
// 'color:' styles. Use text.
|
| 47 |
else if ($text && preg_match('/(?<!-)color[^{:]*:[^{#]*$/i', $chunk)) {
|
| 48 |
$base = 'text';
|
| 49 |
}
|
| 50 |
// Reset back to base.
|
| 51 |
else {
|
| 52 |
$base = 'base';
|
| 53 |
}
|
| 54 |
}
|
| 55 |
$output .= $chunk;
|
| 56 |
$is_color = !$is_color;
|
| 57 |
}
|
| 58 |
|
| 59 |
|
| 60 |
$style = $output;
|
| 61 |
|
| 62 |
return $style;
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Shift a given color, using a reference pair and a target blend color.
|
| 67 |
*
|
| 68 |
* Note: this function is significantly different from the JS version, as it
|
| 69 |
* is written to match the blended images perfectly.
|
| 70 |
*
|
| 71 |
* Constraint: if (ref2 == target + (ref1 - target) * delta) for some fraction delta
|
| 72 |
* then (return == target + (given - target) * delta)
|
| 73 |
*
|
| 74 |
* Loose constraint: Preserve relative positions in saturation and luminance
|
| 75 |
* space.
|
| 76 |
*/
|
| 77 |
function _color_shift($given, $ref1, $ref2, $target) {
|
| 78 |
|
| 79 |
module_load_include('inc', 'color', 'color.algorithms');
|
| 80 |
|
| 81 |
// We assume that ref2 is a blend of ref1 and target and find
|
| 82 |
// delta based on the length of the difference vectors:
|
| 83 |
|
| 84 |
// delta = 1 - |ref2 - ref1| / |white - ref1|
|
| 85 |
$target = _color_unpack($target, TRUE);
|
| 86 |
$ref1 = _color_unpack($ref1, TRUE);
|
| 87 |
$ref2 = _color_unpack($ref2, TRUE);
|
| 88 |
$numerator = 0;
|
| 89 |
$denominator = 0;
|
| 90 |
for ($i = 0; $i < 3; ++$i) {
|
| 91 |
$numerator += ($ref2[$i] - $ref1[$i]) * ($ref2[$i] - $ref1[$i]);
|
| 92 |
$denominator += ($target[$i] - $ref1[$i]) * ($target[$i] - $ref1[$i]);
|
| 93 |
}
|
| 94 |
$delta = ($denominator > 0) ? (1 - sqrt($numerator / $denominator)) : 0;
|
| 95 |
|
| 96 |
// Calculate the color that ref2 would be if the assumption was TRUE.
|
| 97 |
for ($i = 0; $i < 3; ++$i) {
|
| 98 |
$ref3[$i] = $target[$i] + ($ref1[$i] - $target[$i]) * $delta;
|
| 99 |
}
|
| 100 |
|
| 101 |
// If the assumption is not TRUE, there is a difference between ref2 and ref3.
|
| 102 |
// We measure this in HSL space. Notation: x' = hsl(x).
|
| 103 |
$ref2 = _color_rgb2hsl($ref2);
|
| 104 |
$ref3 = _color_rgb2hsl($ref3);
|
| 105 |
for ($i = 0; $i < 3; ++$i) {
|
| 106 |
$shift[$i] = $ref2[$i] - $ref3[$i];
|
| 107 |
}
|
| 108 |
|
| 109 |
// Take the given color, and blend it towards the target.
|
| 110 |
$given = _color_unpack($given, TRUE);
|
| 111 |
for ($i = 0; $i < 3; ++$i) {
|
| 112 |
$result[$i] = $target[$i] + ($given[$i] - $target[$i]) * $delta;
|
| 113 |
}
|
| 114 |
|
| 115 |
// Finally, we apply the extra shift in HSL space.
|
| 116 |
// Note: if ref2 is a pure blend of ref1 and target, then |shift| = 0.
|
| 117 |
$result = _color_rgb2hsl($result);
|
| 118 |
for ($i = 0; $i < 3; ++$i) {
|
| 119 |
$result[$i] = min(1, max(0, $result[$i] + $shift[$i]));
|
| 120 |
}
|
| 121 |
$result = _color_hsl2rgb($result);
|
| 122 |
|
| 123 |
// Return hex color.
|
| 124 |
return _color_pack($result, TRUE);
|
| 125 |
}
|