PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Faker/Provider/Color.php

http://github.com/fzaninotto/Faker
PHP | 143 lines | 93 code | 15 blank | 35 comment | 0 complexity | 904e2f53951821399de827caef55b2d7 MD5 | raw file
  1. <?php
  2. namespace Faker\Provider;
  3. /**
  4. * @author lsv
  5. */
  6. class Color extends Base
  7. {
  8. protected static $safeColorNames = array(
  9. 'black', 'maroon', 'green', 'navy', 'olive',
  10. 'purple', 'teal', 'lime', 'blue', 'silver',
  11. 'gray', 'yellow', 'fuchsia', 'aqua', 'white'
  12. );
  13. protected static $allColorNames = array(
  14. 'AliceBlue', 'AntiqueWhite', 'Aqua', 'Aquamarine',
  15. 'Azure', 'Beige', 'Bisque', 'Black', 'BlanchedAlmond',
  16. 'Blue', 'BlueViolet', 'Brown', 'BurlyWood', 'CadetBlue',
  17. 'Chartreuse', 'Chocolate', 'Coral', 'CornflowerBlue',
  18. 'Cornsilk', 'Crimson', 'Cyan', 'DarkBlue', 'DarkCyan',
  19. 'DarkGoldenRod', 'DarkGray', 'DarkGreen', 'DarkKhaki',
  20. 'DarkMagenta', 'DarkOliveGreen', 'Darkorange', 'DarkOrchid',
  21. 'DarkRed', 'DarkSalmon', 'DarkSeaGreen', 'DarkSlateBlue',
  22. 'DarkSlateGray', 'DarkTurquoise', 'DarkViolet', 'DeepPink',
  23. 'DeepSkyBlue', 'DimGray', 'DimGrey', 'DodgerBlue', 'FireBrick',
  24. 'FloralWhite', 'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite',
  25. 'Gold', 'GoldenRod', 'Gray', 'Green', 'GreenYellow', 'HoneyDew',
  26. 'HotPink', 'IndianRed', 'Indigo', 'Ivory', 'Khaki', 'Lavender',
  27. 'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue', 'LightCoral',
  28. 'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGreen', 'LightPink',
  29. 'LightSalmon', 'LightSeaGreen', 'LightSkyBlue', 'LightSlateGray', 'LightSteelBlue',
  30. 'LightYellow', 'Lime', 'LimeGreen', 'Linen', 'Magenta', 'Maroon', 'MediumAquaMarine',
  31. 'MediumBlue', 'MediumOrchid', 'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue',
  32. 'MediumSpringGreen', 'MediumTurquoise', 'MediumVioletRed', 'MidnightBlue',
  33. 'MintCream', 'MistyRose', 'Moccasin', 'NavajoWhite', 'Navy', 'OldLace', 'Olive',
  34. 'OliveDrab', 'Orange', 'OrangeRed', 'Orchid', 'PaleGoldenRod', 'PaleGreen',
  35. 'PaleTurquoise', 'PaleVioletRed', 'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum',
  36. 'PowderBlue', 'Purple', 'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Salmon',
  37. 'SandyBrown', 'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue',
  38. 'SlateGray', 'Snow', 'SpringGreen', 'SteelBlue', 'Tan', 'Teal', 'Thistle', 'Tomato',
  39. 'Turquoise', 'Violet', 'Wheat', 'White', 'WhiteSmoke', 'Yellow', 'YellowGreen'
  40. );
  41. /**
  42. * @example '#fa3cc2'
  43. */
  44. public static function hexColor()
  45. {
  46. return '#' . str_pad(dechex(mt_rand(1, 16777215)), 6, '0', STR_PAD_LEFT);
  47. }
  48. /**
  49. * @example '#ff0044'
  50. */
  51. public static function safeHexColor()
  52. {
  53. $color = str_pad(dechex(mt_rand(0, 255)), 3, '0', STR_PAD_LEFT);
  54. return '#' . $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
  55. }
  56. /**
  57. * @example 'array(0,255,122)'
  58. */
  59. public static function rgbColorAsArray()
  60. {
  61. $color = static::hexColor();
  62. return array(
  63. hexdec(substr($color, 1, 2)),
  64. hexdec(substr($color, 3, 2)),
  65. hexdec(substr($color, 5, 2))
  66. );
  67. }
  68. /**
  69. * @example '0,255,122'
  70. */
  71. public static function rgbColor()
  72. {
  73. return implode(',', static::rgbColorAsArray());
  74. }
  75. /**
  76. * @example 'rgb(0,255,122)'
  77. */
  78. public static function rgbCssColor()
  79. {
  80. return 'rgb(' . static::rgbColor() . ')';
  81. }
  82. /**
  83. * @example 'rgba(0,255,122,0.8)'
  84. */
  85. public static function rgbaCssColor()
  86. {
  87. return 'rgba(' . static::rgbColor() . ',' . static::randomFloat(1, 0, 1) . ')';
  88. }
  89. /**
  90. * @example 'blue'
  91. */
  92. public static function safeColorName()
  93. {
  94. return static::randomElement(static::$safeColorNames);
  95. }
  96. /**
  97. * @example 'NavajoWhite'
  98. */
  99. public static function colorName()
  100. {
  101. return static::randomElement(static::$allColorNames);
  102. }
  103. /**
  104. * @example '340,50,20'
  105. * @return string
  106. */
  107. public static function hslColor()
  108. {
  109. return sprintf(
  110. '%s,%s,%s',
  111. static::numberBetween(0, 360),
  112. static::numberBetween(0, 100),
  113. static::numberBetween(0, 100)
  114. );
  115. }
  116. /**
  117. * @example array(340, 50, 20)
  118. * @return array
  119. */
  120. public static function hslColorAsArray()
  121. {
  122. return array(
  123. static::numberBetween(0, 360),
  124. static::numberBetween(0, 100),
  125. static::numberBetween(0, 100)
  126. );
  127. }
  128. }