PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/data/wpcom-themes/duotone/inc/csscolor.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests
PHP | 423 lines | 219 code | 74 blank | 130 comment | 36 complexity | 79206e908e4c9de04f62528afb3ff8c7 MD5 | raw file
  1. <?php
  2. /*
  3. csscolor.php
  4. Copyright 2004 Patrick Fitzgerald
  5. http://www.barelyfitz.com/projects/csscolor/
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. define('CSS_COLOR_ERROR', 100);
  19. class CSS_Color
  20. {
  21. //==================================================
  22. //==PARAMETERS======================================
  23. //==================================================
  24. // $this->bg = array of CSS color values
  25. // $this->bg[0] is the bg color
  26. // $this->bg['+1'..'+5'] are lighter colors
  27. // $this->bg['-1'..'-5'] are darker colors
  28. var $bg = array();
  29. // $this->fg = array of foreground colors.
  30. // Each color corresponds to a background color.
  31. var $fg = array();
  32. // brightDiff is the minimum brightness difference
  33. // between the background and the foreground.
  34. // Note: you should not change this directly,
  35. // instead use setBrightDiff() and getBrightDiff()
  36. var $minBrightDiff = 128;
  37. // colorDiff is the minimum color difference
  38. // between the background and the foreground.
  39. // Note: you should not change this directly,
  40. // instead use setColorDiff() and getColorDiff()
  41. var $minColorDiff = 100;
  42. //==================================================
  43. //==CONSTRUCTOR=====================================
  44. //==================================================
  45. function CSS_Color($bgHex, $fgHex='')
  46. {
  47. // This is the constructor method for the class,
  48. // which is called when a new object is created.
  49. // Initialize this PEAR object so I can
  50. // use the PEAR error return mechanism
  51. // Initialize the palette
  52. $this->setPalette($bgHex, $fgHex);
  53. }
  54. //==================================================
  55. //==METHODS=========================================
  56. //==================================================
  57. //--------------------------------------------------
  58. function setPalette($bgHex, $fgHex = '')
  59. {
  60. // Initialize the color palettes
  61. // If a foreground color was not specified,
  62. // just use the background color.
  63. if (!$fgHex) {
  64. $fgHex = $bgHex;
  65. }
  66. // Clear the existing palette
  67. $this->bg = array();
  68. $this->fg = array();
  69. // Make sure we got a valid hex value
  70. if (!$this->isHex($bgHex)) {
  71. error_log("background color '$bgHex' is not a hex color value");
  72. return false;
  73. }
  74. // Set the bg color
  75. $this->bg[0] = $bgHex;
  76. $this->bg['+1'] = $this->lighten($bgHex, .85);
  77. $this->bg['+2'] = $this->lighten($bgHex, .75);
  78. $this->bg['+3'] = $this->lighten($bgHex, .5);
  79. $this->bg['+4'] = $this->lighten($bgHex, .25);
  80. $this->bg['+5'] = $this->lighten($bgHex, .1);
  81. $this->bg['-1'] = $this->darken($bgHex, .85);
  82. $this->bg['-2'] = $this->darken($bgHex, .75);
  83. $this->bg['-3'] = $this->darken($bgHex, .5);
  84. $this->bg['-4'] = $this->darken($bgHex, .25);
  85. $this->bg['-5'] = $this->darken($bgHex, .1);
  86. // Make sure we got a valid hex value
  87. if (!$this->isHex($fgHex)) {
  88. $this->raiseError("background color '$bgHex' is not a hex color value",
  89. __FUNCTION__, __LINE__);
  90. return false;
  91. }
  92. // Set up the foreground colors
  93. $this->fg[0] = $this->calcFG( $this->bg[0], $fgHex);
  94. $this->fg['+1'] = $this->calcFG( $this->bg['+1'], $fgHex);
  95. $this->fg['+2'] = $this->calcFG( $this->bg['+2'], $fgHex);
  96. $this->fg['+3'] = $this->calcFG( $this->bg['+3'], $fgHex);
  97. $this->fg['+4'] = $this->calcFG( $this->bg['+4'], $fgHex);
  98. $this->fg['+5'] = $this->calcFG( $this->bg['+5'], $fgHex);
  99. $this->fg['-1'] = $this->calcFG( $this->bg['-1'], $fgHex);
  100. $this->fg['-2'] = $this->calcFG( $this->bg['-2'], $fgHex);
  101. $this->fg['-3'] = $this->calcFG( $this->bg['-3'], $fgHex);
  102. $this->fg['-4'] = $this->calcFG( $this->bg['-4'], $fgHex);
  103. $this->fg['-5'] = $this->calcFG( $this->bg['-5'], $fgHex);
  104. }
  105. //--------------------------------------------------
  106. function lighten($hex, $percent)
  107. {
  108. return $this->mix($hex, $percent, 255);
  109. }
  110. //--------------------------------------------------
  111. function darken($hex, $percent)
  112. {
  113. return $this->mix($hex, $percent, 0);
  114. }
  115. //--------------------------------------------------
  116. function mix($hex, $percent, $mask)
  117. {
  118. // Make sure inputs are valid
  119. if (!is_numeric($percent) || $percent < 0 || $percent > 1) {
  120. $this->raiseError("percent=$percent is not valid",
  121. __FUNCTION__, __LINE__);
  122. return false;
  123. }
  124. if (!is_int($mask) || $mask < 0 || $mask > 255) {
  125. $this->raiseError("mask=$mask is not valid",
  126. __FUNCTION__, __LINE__);
  127. return false;
  128. }
  129. $rgb = $this->hex2RGB($hex);
  130. if (!is_array($rgb)) {
  131. // hex2RGB will raise an error
  132. return false;
  133. }
  134. for ($i=0; $i<3; $i++) {
  135. $rgb[$i] = round($rgb[$i] * $percent) + round($mask * (1-$percent));
  136. // In case rounding up causes us to go to 256
  137. if ($rgb[$i] > 255) {
  138. $rgb[$i] = 255;
  139. }
  140. }
  141. return $this->RGB2Hex($rgb);
  142. }
  143. //--------------------------------------------------
  144. function hex2RGB($hex)
  145. {
  146. //
  147. // Given a hex color (rrggbb or rgb),
  148. // returns an array (r, g, b) with decimal values
  149. // If $hex is not the correct format,
  150. // returns false.
  151. //
  152. // example:
  153. // $d = hex2RGB('#abc');
  154. // if (!$d) { error }
  155. // Regexp for a valid hex digit
  156. $d = '[a-fA-F0-9]';
  157. // Make sure $hex is valid
  158. if (preg_match("/^($d$d)($d$d)($d$d)\$/", $hex, $rgb)) {
  159. return array(
  160. hexdec($rgb[1]),
  161. hexdec($rgb[2]),
  162. hexdec($rgb[3])
  163. );
  164. }
  165. if (preg_match("/^($d)($d)($d)$/", $hex, $rgb)) {
  166. return array(
  167. hexdec($rgb[1] . $rgb[1]),
  168. hexdec($rgb[2] . $rgb[2]),
  169. hexdec($rgb[3] . $rgb[3])
  170. );
  171. }
  172. $this->raiseError("cannot convert hex '$hex' to RGB", __FUNCTION__, __LINE__);
  173. return false;
  174. }
  175. //--------------------------------------------------
  176. function RGB2Hex($rgb)
  177. {
  178. // Given an array(rval,gval,bval) consisting of
  179. // decimal color values (0-255), returns a hex string
  180. // suitable for use with CSS.
  181. // Returns false if the input is not in the correct format.
  182. // Example:
  183. // $h = RGB2Hex(array(255,0,255));
  184. // if (!$h) { error };
  185. // Make sure the input is valid
  186. if(!$this->isRGB($rgb)) {
  187. $this->raiseError("RGB value is not valid", __FUNCTION__, __LINE__);
  188. return false;
  189. }
  190. $hex = "";
  191. for($i=0; $i < 3; $i++) {
  192. // Convert the decimal digit to hex
  193. $hexDigit = dechex($rgb[$i]);
  194. // Add a leading zero if necessary
  195. if(strlen($hexDigit) == 1) {
  196. $hexDigit = "0" . $hexDigit;
  197. }
  198. // Append to the hex string
  199. $hex .= $hexDigit;
  200. }
  201. // Return the complete hex string
  202. return $hex;
  203. }
  204. //--------------------------------------------------
  205. function isHex($hex)
  206. {
  207. // Returns true if $hex is a valid CSS hex color.
  208. // The "#" character at the start is optional.
  209. // Regexp for a valid hex digit
  210. $d = '[a-fA-F0-9]';
  211. // Make sure $hex is valid
  212. if (preg_match("/^#?$d$d$d$d$d$d\$/", $hex) ||
  213. preg_match("/^#?$d$d$d\$/", $hex)) {
  214. return true;
  215. }
  216. return false;
  217. }
  218. //--------------------------------------------------
  219. function isRGB($rgb)
  220. {
  221. // Returns true if $rgb is an array with three valid
  222. // decimal color digits.
  223. if (!is_array($rgb) || count($rgb) != 3) {
  224. return false;
  225. }
  226. for($i=0; $i < 3; $i++) {
  227. // Get the decimal digit
  228. $dec = intval($rgb[$i]);
  229. // Make sure the decimal digit is between 0 and 255
  230. if (!is_int($dec) || $dec < 0 || $dec > 255) {
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. //--------------------------------------------------
  237. function calcFG($bgHex, $fgHex)
  238. {
  239. // Given a background color $bgHex and a foreground color $fgHex,
  240. // modifies the foreground color so it will have enough contrast
  241. // to be seen against the background color.
  242. //
  243. // The following parameters are used:
  244. // $this->minBrightDiff
  245. // $this->minColorDiff
  246. // Loop through brighter and darker versions
  247. // of the foreground color.
  248. // The numbers here represent the amount of
  249. // foreground color to mix with black and white.
  250. foreach (array(1, 0.75, 0.5, 0.25, 0) as $percent) {
  251. $darker = $this->darken($fgHex, $percent);
  252. $lighter = $this->lighten($fgHex, $percent);
  253. $darkerBrightDiff = $this->brightnessDiff($bgHex, $darker);
  254. $lighterBrightDiff = $this->brightnessDiff($bgHex, $lighter);
  255. if ($lighterBrightDiff > $darkerBrightDiff) {
  256. $newFG = $lighter;
  257. $newFGBrightDiff = $lighterBrightDiff;
  258. } else {
  259. $newFG = $darker;
  260. $newFGBrightDiff = $darkerBrightDiff;
  261. }
  262. $newFGColorDiff = $this->colorDiff($bgHex, $newFG);
  263. if ($newFGBrightDiff >= $this->minBrightDiff &&
  264. $newFGColorDiff >= $this->minColorDiff) {
  265. break;
  266. }
  267. }
  268. return $newFG;
  269. }
  270. //--------------------------------------------------
  271. function getMinBrightDiff()
  272. {
  273. return $this->minBrightDiff;
  274. }
  275. function setMinBrightDiff($b, $resetPalette = true)
  276. {
  277. $this->minBrightDiff = $b;
  278. if ($resetPalette) {
  279. $this->setPalette($this->bg[0],$this->fg[0]);
  280. }
  281. }
  282. //--------------------------------------------------
  283. function getMinColorDiff()
  284. {
  285. return $this->minColorDiff;
  286. }
  287. function setMinColorDiff($d, $resetPalette = true)
  288. {
  289. $this->minColorDiff = $d;
  290. if ($resetPalette) {
  291. $this->setPalette($this->bg[0],$this->fg[0]);
  292. }
  293. }
  294. //--------------------------------------------------
  295. function brightness($hex)
  296. {
  297. // Returns the brightness value for a color,
  298. // a number between zero and 178.
  299. // To allow for maximum readability, the difference between
  300. // the background brightness and the foreground brightness
  301. // should be greater than 125.
  302. $rgb = $this->hex2RGB($hex);
  303. if (!is_array($rgb)) {
  304. // hex2RGB will raise an error
  305. return false;
  306. }
  307. return( (($rgb[0] * 299) + ($rgb[1] * 587) + ($rgb[2] * 114)) / 1000 );
  308. }
  309. //--------------------------------------------------
  310. function brightnessDiff($hex1, $hex2)
  311. {
  312. // Returns the brightness value for a color,
  313. // a number between zero and 178.
  314. // To allow for maximum readability, the difference between
  315. // the background brightness and the foreground brightness
  316. // should be greater than 125.
  317. $b1 = $this->brightness($hex1);
  318. $b2 = $this->brightness($hex2);
  319. if (is_bool($b1) || is_bool($b2)) {
  320. return false;
  321. }
  322. return abs($b1 - $b2);
  323. }
  324. //--------------------------------------------------
  325. function colorDiff($hex1, $hex2)
  326. {
  327. // Returns the contrast between two colors,
  328. // an integer between 0 and 675.
  329. // To allow for maximum readability, the difference between
  330. // the background and the foreground color should be > 500.
  331. $rgb1 = $this->hex2RGB($hex1);
  332. $rgb2 = $this->hex2RGB($hex2);
  333. if (!is_array($rgb1) || !is_array($rgb2)) {
  334. // hex2RGB will raise an error
  335. return -1;
  336. }
  337. $r1 = $rgb1[0];
  338. $g1 = $rgb1[1];
  339. $b1 = $rgb1[2];
  340. $r2 = $rgb2[0];
  341. $g2 = $rgb2[1];
  342. $b2 = $rgb2[2];
  343. return(abs($r1-$r2) + abs($g1-$g2) + abs($b1-$b2));
  344. }
  345. }
  346. ?>