PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Faker/Provider/Color.php

https://github.com/JhonnyL/Faker
PHP | 108 lines | 72 code | 12 blank | 24 comment | 0 complexity | 51a8c4a0a9e0b36d62053d115654e32f 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 'blue'
  84. */
  85. public static function safeColorName()
  86. {
  87. return static::randomElement(static::$safeColorNames);
  88. }
  89. /**
  90. * @example 'NavajoWhite'
  91. */
  92. public static function colorName()
  93. {
  94. return static::randomElement(static::$allColorNames);
  95. }
  96. }