Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

colors.ts 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /* eslint-disable no-bitwise, no-mixed-operators */
  21. export function stringToColor(str: string) {
  22. let hash = 0;
  23. for (let i = 0; i < str.length; i++) {
  24. hash = str.charCodeAt(i) + ((hash << 5) - hash);
  25. }
  26. let color = '#';
  27. for (let i = 0; i < 3; i++) {
  28. const value = (hash >> (i * 8)) & 0xff;
  29. color += ('00' + value.toString(16)).substr(-2);
  30. }
  31. return color;
  32. }
  33. export function isDarkColor(color: string) {
  34. color = color.substr(1);
  35. if (color.length === 3) {
  36. // shortcut notation: #f90
  37. color = color[0] + color[0] + color[1] + color[1] + color[2] + color[2];
  38. }
  39. const rgb = parseInt(color.substr(1), 16);
  40. const r = (rgb >> 16) & 0xff;
  41. const g = (rgb >> 8) & 0xff;
  42. const b = (rgb >> 0) & 0xff;
  43. const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
  44. return luma < 140;
  45. }
  46. export function getTextColor(background: string, dark = '#222', light = '#fff') {
  47. return isDarkColor(background) ? light : dark;
  48. }