webcolor.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Base class for deleting things
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Personal
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. class WebColor {
  33. // XXX: Maybe make getters and setters for r,g,b values and tuples,
  34. // e.g.: to support this kinda CSS representation: rgb(255,0,0)
  35. // http://www.w3.org/TR/CSS21/syndata.html#color-units
  36. var $red = 0;
  37. var $green = 0;
  38. var $blue = 0;
  39. /**
  40. * Constructor
  41. *
  42. * @return nothing
  43. */
  44. function __construct($color = null)
  45. {
  46. if (isset($color)) {
  47. $this->parseColor($color);
  48. }
  49. }
  50. /**
  51. * Parses input to and tries to determine whether the color
  52. * is being specified via an integer or hex tuple and sets
  53. * the RGB instance variables accordingly.
  54. *
  55. * XXX: Maybe support (r,g,b) style, and array?
  56. *
  57. * @param mixed $color
  58. *
  59. * @return nothing
  60. */
  61. function parseColor($color) {
  62. if (is_numeric($color)) {
  63. $this->setIntColor($color);
  64. } else {
  65. // XXX named colors
  66. // XXX: probably should do even more validation
  67. if (preg_match('/(#([0-9A-Fa-f]{3,6})\b)/u', $color) > 0) {
  68. $this->setHexColor($color);
  69. } else {
  70. // TRANS: Web color exception thrown when a hexadecimal color code does not validate.
  71. // TRANS: %s is the provided (invalid) color code.
  72. $errmsg = _('%s is not a valid color! Use 3 or 6 hex characters.');
  73. throw new WebColorException(sprintf($errmsg, $color));
  74. }
  75. }
  76. }
  77. /**
  78. * @param string $name
  79. *
  80. * @return nothing
  81. */
  82. function setNamedColor($name)
  83. {
  84. // XXX Implement this
  85. }
  86. /**
  87. * Sets the RGB color values from a a hex tuple
  88. *
  89. * @param string $hexcolor
  90. *
  91. * @return nothing
  92. */
  93. function setHexColor($hexcolor) {
  94. if ($hexcolor[0] == '#') {
  95. $hexcolor = substr($hexcolor, 1);
  96. }
  97. if (strlen($hexcolor) == 6) {
  98. list($r, $g, $b) = array($hexcolor[0].$hexcolor[1],
  99. $hexcolor[2].$hexcolor[3],
  100. $hexcolor[4].$hexcolor[5]);
  101. } elseif (strlen($hexcolor) == 3) {
  102. list($r, $g, $b) = array($hexcolor[0].$hexcolor[0],
  103. $hexcolor[1].$hexcolor[1],
  104. $hexcolor[2].$hexcolor[2]);
  105. } else {
  106. // TRANS: Web color exception thrown when a hexadecimal color code does not validate.
  107. // TRANS: %s is the provided (invalid) color code.
  108. $errmsg = _('%s is not a valid color! Use 3 or 6 hex characters.');
  109. throw new WebColorException(sprintf($errmsg, $hexcolor));
  110. }
  111. $this->red = hexdec($r);
  112. $this->green = hexdec($g);
  113. $this->blue = hexdec($b);
  114. }
  115. /**
  116. * Sets the RGB color values from a 24-bit integer
  117. *
  118. * @param int $intcolor
  119. *
  120. * @return nothing
  121. */
  122. function setIntColor($intcolor)
  123. {
  124. // We could do 32 bit and have an alpha channel because
  125. // Sarven wants one real bad, but nah.
  126. $this->red = $intcolor >> 16;
  127. $this->green = $intcolor >> 8 & 0xFF;
  128. $this->blue = $intcolor & 0xFF;
  129. }
  130. /**
  131. * Returns a hex tuple of the RGB color useful for output in HTML
  132. *
  133. * @return string
  134. */
  135. function hexValue() {
  136. $hexcolor = (strlen(dechex($this->red)) < 2 ? '0' : '' ) .
  137. dechex($this->red);
  138. $hexcolor .= (strlen(dechex($this->green)) < 2 ? '0' : '') .
  139. dechex($this->green);
  140. $hexcolor .= (strlen(dechex($this->blue)) < 2 ? '0' : '') .
  141. dechex($this->blue);
  142. return strtoupper($hexcolor);
  143. }
  144. /**
  145. * Returns a 24-bit packed integer representation of the RGB color
  146. * for convenient storage in the DB
  147. *
  148. * XXX: probably could just use hexdec() instead
  149. *
  150. * @return int
  151. */
  152. function intValue()
  153. {
  154. $intcolor = 256 * 256 * $this->red + 256 * $this->green + $this->blue;
  155. return $intcolor;
  156. }
  157. }
  158. class WebColorException extends Exception
  159. {
  160. }