Herlper.php 554 B

12345678910111213141516171819202122232425
  1. <?php
  2. namespace UCore\DcatAdmin;
  3. class Herlper
  4. {
  5. /**
  6. * 根据背景色获取对比色
  7. *
  8. * @param string $hexColor
  9. * @return string
  10. */
  11. public static function getContrastColor(string $hexColor): string
  12. {
  13. $hexColor = ltrim($hexColor, '#');
  14. $r = hexdec(substr($hexColor, 0, 2));
  15. $g = hexdec(substr($hexColor, 2, 2));
  16. $b = hexdec(substr($hexColor, 4, 2));
  17. $brightness = ($r * 299 + $g * 587 + $b * 114) / 1000;
  18. return $brightness > 128 ? '#000000' : '#ffffff';
  19. }
  20. }