PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/color.php

https://github.com/gadgad/PoP-AS-Visualization
PHP | 171 lines | 147 code | 15 blank | 9 comment | 12 complexity | 758940609b7e05a45fe2be5ee2dbcf3b MD5 | raw file
  1. <?php
  2. /*
  3. * represents a single color (by red,green,blue & alpha values)
  4. * values can eitgher be set manually by the user or chosen randomly
  5. * supports two types of output formats (web-format/google-maps-format).
  6. */
  7. class Color
  8. {
  9. public $trans;
  10. public $red;
  11. public $green;
  12. public $blue;
  13. public $red_hex;
  14. public $green_hex;
  15. public $blue_hex;
  16. public $trans_hex;
  17. public function __construct()
  18. {
  19. $this->randColor();
  20. if(func_num_args()==3 || func_num_args()==4)
  21. {
  22. $r = func_get_arg(0);
  23. $g = func_get_arg(1);
  24. $b = func_get_arg(2);
  25. $this->setColor($r, $g, $b);
  26. if(func_num_args()==4) $this->setTrans(func_get_arg(3));
  27. } elseif (func_num_args()==1) {
  28. $webf = func_get_arg(0);
  29. if(strlen($webf)==6){
  30. $this->setWebColor($webf);
  31. }
  32. if(strlen($webf)==7 && (substr($webf,0,1)=="#")){
  33. $webf = trim($webf,'#');
  34. $this->setWebColor($webf);
  35. }
  36. }
  37. }
  38. function randColor()
  39. {
  40. $this->red = rand(0,255);
  41. $this->green = rand(0,255);
  42. $this->blue = rand(0,255);
  43. $this->trans = 150;
  44. }
  45. public function setColor($r,$g,$b)
  46. {
  47. $this->red = $r;
  48. $this->green = $g;
  49. $this->blue = $b;
  50. $this->Dec2Hex();
  51. }
  52. public function setWebColor($webf)
  53. {
  54. $this->red = hexdec(substr($webf,0,2));
  55. $this->green = hexdec(substr($webf,2,2));
  56. $this->blue = hexdec(substr($webf,4,2));
  57. $this->Dec2Hex();
  58. }
  59. public function setTrans($t)
  60. {
  61. $this->trans = $t;
  62. $this->Dec2Hex();
  63. }
  64. function Dec2Hex()
  65. {
  66. $this->red_hex = ($this->red<16)? '0'.dechex($this->red):dechex($this->red);
  67. $this->green_hex = ($this->green<16)? '0'.dechex($this->green):dechex($this->green);
  68. $this->blue_hex = ($this->blue<16)? '0'.dechex($this->blue):dechex($this->blue);
  69. $this->trans_hex = ($this->trans<16)? '0'.dechex($this->trans):dechex($this->trans);
  70. }
  71. public function gm_format()
  72. {
  73. return ($this->trans_hex).($this->blue_hex).($this->green_hex).($this->red_hex);
  74. }
  75. public function web_format()
  76. {
  77. return ($this->red_hex).($this->green_hex).($this->blue_hex);
  78. }
  79. public function calc_dist()
  80. {
  81. $r2 = $g2 = $b2 = 0;
  82. if(func_num_args()==3) {
  83. $r2 = func_get_arg(0);
  84. $g2 = func_get_arg(1);
  85. $b2 = func_get_arg(2);
  86. } else if(func_num_args() == 1) {
  87. $r2 = func_get_arg(0)->red;
  88. $g2 = func_get_arg(0)->green;
  89. $b2 = func_get_arg(0)->blue;
  90. }
  91. $dr = $this->red - $r2;
  92. $dg = $this->green - $g2;
  93. $db = $this->blue - $b2;
  94. return sqrt(pow($dr,2)+pow($dg,2)+pow($db,2));
  95. }
  96. }
  97. /*
  98. * a random-color-picker for 'web-safe' colors only
  99. * this interface is no longer being used..
  100. */
  101. class ColorPicker
  102. {
  103. public $safe_colors;
  104. public $color_list;
  105. private static $counter = 0;
  106. public function __construct($num)
  107. {
  108. $this->init_safe_colors();
  109. $this->init_color_list($num);
  110. }
  111. function init_safe_colors()
  112. {
  113. $this->safe_colors = array();
  114. $c=array('00','33','66','99','CC','FF');
  115. foreach ($c as $x) {
  116. foreach ($c as $y) {
  117. foreach ($c as $z) {
  118. $r = hexdec($x);
  119. $g = hexdec($y);
  120. $b = hexdec($z);
  121. $color = new Color($r,$g,$b);
  122. $this->safe_colors[] = $color;
  123. }
  124. }
  125. }
  126. }
  127. function init_color_list($num)
  128. {
  129. $range = 216;
  130. $offset = rand(0,$range-1);
  131. $delta = floor($range/$num);
  132. $this->color_list=array_fill(0, $num, NULL);
  133. for($i=0;$i<$num;$i++)
  134. {
  135. $rand_steps = rand(0,$num-$i-1);
  136. $j = 0;
  137. $index = 0;
  138. while($this->color_list[$index]!= NULL){
  139. $index++;
  140. }
  141. while($j<$rand_steps)
  142. {
  143. $index++;
  144. $j++;
  145. if($this->color_list[$index]!= NULL)
  146. $index++;
  147. }
  148. $this->color_list[$index] = $this->safe_colors[($offset+$i*$delta)%$range];
  149. }
  150. }
  151. function getColor()
  152. {
  153. return $this->color_list[self::$counter++];
  154. }
  155. }
  156. ?>