PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/palette.php

https://github.com/mrahmadt/PHP_Word_Cloud
PHP | 66 lines | 39 code | 6 blank | 21 comment | 4 complexity | 19c2d571c6e3c981b2b63d096d43b056 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the PHP_Word_Cloud project.
  4. * http://github.com/sixty-nine/PHP_Word_Cloud
  5. *
  6. * @author Daniel Barsotti / dan [at] dreamcraft [dot] ch
  7. * @license http://creativecommons.org/licenses/by-nc-sa/3.0/
  8. * Creative Commons Attribution-NonCommercial-ShareAlike 3.0
  9. */
  10. /**
  11. * Generate color palettes (arrays of allocated colors)
  12. */
  13. class Palette {
  14. private static $palettes = array(
  15. 'aqua' => array('BED661', '89E894', '78D5E3', '7AF5F5', '34DDDD', '93E2D5'),
  16. 'yellow/blue' => array('FFCC00', 'CCCCCC', '666699'),
  17. 'grey' => array('87907D', 'AAB6A2', '555555', '666666'),
  18. 'brown' => array('CC6600', 'FFFBD0', 'FF9900', 'C13100'),
  19. 'army' => array('595F23', '829F53', 'A2B964', '5F1E02', 'E15417', 'FCF141'),
  20. 'pastel' => array('EF597B', 'FF6D31', '73B66B', 'FFCB18', '29A2C6'),
  21. 'red' => array('FFFF66', 'FFCC00', 'FF9900', 'FF0000'),
  22. );
  23. /**
  24. * Construct a random color palette
  25. * @param object $im The GD image
  26. * @param integer $count The number of colors in the palette
  27. */
  28. public static function get_random_palette($im, $count = 5) {
  29. $palette = array();
  30. for ($i = 0; $i < $count; $i++) {
  31. $palette[] = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
  32. }
  33. return $palette;
  34. }
  35. /**
  36. * Construct a color palette from a list of hexadecimal colors (RRGGBB)
  37. * @param object $im The GD image
  38. * @param array $hex_array An array of hexadecimal color strings
  39. */
  40. public static function get_palette_from_hex($im, $hex_array) {
  41. $palette = array();
  42. foreach($hex_array as $hex) {
  43. if (strlen($hex) != 6) throw new Exception("Invalid palette color '$hex'");
  44. $palette[] = imagecolorallocate($im,
  45. hexdec(substr($hex, 0, 2)),
  46. hexdec(substr($hex, 2, 2)),
  47. hexdec(substr($hex, 4, 2)));
  48. }
  49. return $palette;
  50. }
  51. public static function get_named_palette($im, $name) {
  52. if (array_key_exists($name, self::$palettes)) {
  53. return self::get_palette_from_hex($im, self::$palettes[$name]);
  54. }
  55. return self::get_named_palette($im, 'grey');
  56. }
  57. public static function list_named_palettes() {
  58. return array_keys(self::$palettes);
  59. }
  60. }