BCGColor.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Holds Color in RGB Format.
  6. *
  7. *--------------------------------------------------------------------
  8. * Copyright (C) Jean-Sebastien Goupil
  9. * http://www.barcodephp.com
  10. */
  11. class BCGColor {
  12. protected $r, $g, $b; // int Hexadecimal Value
  13. protected $transparent;
  14. /**
  15. * Save RGB value into the classes.
  16. *
  17. * There are 4 way to associate color with this classes :
  18. * 1. Gives 3 parameters int (R, G, B)
  19. * 2. Gives 1 parameter string hex value (#ff0000) (preceding with #)
  20. * 3. Gives 1 parameter int hex value (0xff0000)
  21. * 4. Gives 1 parameter string color code (white, black, orange...)
  22. *
  23. * @param mixed ...
  24. */
  25. public function __construct() {
  26. $args = func_get_args();
  27. $c = count($args);
  28. if ($c === 3) {
  29. $this->r = intval($args[0]);
  30. $this->g = intval($args[1]);
  31. $this->b = intval($args[2]);
  32. } elseif ($c === 1) {
  33. if (is_string($args[0]) && strlen($args[0]) === 7 && $args[0][0] === '#') { // Hex Value in String
  34. $this->r = intval(substr($args[0], 1, 2), 16);
  35. $this->g = intval(substr($args[0], 3, 2), 16);
  36. $this->b = intval(substr($args[0], 5, 2), 16);
  37. } else {
  38. if (is_string($args[0])) {
  39. $args[0] = self::getColor($args[0]);
  40. }
  41. $args[0] = intval($args[0]);
  42. $this->r = ($args[0] & 0xff0000) >> 16;
  43. $this->g = ($args[0] & 0x00ff00) >> 8;
  44. $this->b = ($args[0] & 0x0000ff);
  45. }
  46. } else {
  47. $this->r = $this->g = $this->b = 0;
  48. }
  49. }
  50. /**
  51. * Sets the color transparent.
  52. *
  53. * @param bool $transparent
  54. */
  55. public function setTransparent($transparent) {
  56. $this->transparent = $transparent;
  57. }
  58. /**
  59. * Returns Red Color.
  60. *
  61. * @return int
  62. */
  63. public function r() {
  64. return $this->r;
  65. }
  66. /**
  67. * Returns Green Color.
  68. *
  69. * @return int
  70. */
  71. public function g() {
  72. return $this->g;
  73. }
  74. /**
  75. * Returns Blue Color.
  76. *
  77. * @return int
  78. */
  79. public function b() {
  80. return $this->b;
  81. }
  82. /**
  83. * Returns the int value for PHP color.
  84. *
  85. * @param resource $im
  86. * @return int
  87. */
  88. public function allocate(&$im) {
  89. $allocated = imagecolorallocate($im, $this->r, $this->g, $this->b);
  90. if ($this->transparent) {
  91. return imagecolortransparent($im, $allocated);
  92. } else {
  93. return $allocated;
  94. }
  95. }
  96. /**
  97. * Returns class of BCGColor depending of the string color.
  98. *
  99. * If the color doens't exist, it takes the default one.
  100. *
  101. * @param string $code
  102. * @param string $default
  103. */
  104. public static function getColor($code, $default = 'white') {
  105. switch(strtolower($code)) {
  106. case '':
  107. case 'white':
  108. return 0xffffff;
  109. case 'black':
  110. return 0x000000;
  111. case 'maroon':
  112. return 0x800000;
  113. case 'red':
  114. return 0xff0000;
  115. case 'orange':
  116. return 0xffa500;
  117. case 'yellow':
  118. return 0xffff00;
  119. case 'olive':
  120. return 0x808000;
  121. case 'purple':
  122. return 0x800080;
  123. case 'fuchsia':
  124. return 0xff00ff;
  125. case 'lime':
  126. return 0x00ff00;
  127. case 'green':
  128. return 0x008000;
  129. case 'navy':
  130. return 0x000080;
  131. case 'blue':
  132. return 0x0000ff;
  133. case 'aqua':
  134. return 0x00ffff;
  135. case 'teal':
  136. return 0x008080;
  137. case 'silver':
  138. return 0xc0c0c0;
  139. case 'gray':
  140. return 0x808080;
  141. default:
  142. return self::getColor($default, 'white');
  143. }
  144. }
  145. }
  146. ?>