obs-proxy-style.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "obs-proxy-style.hpp"
  2. #include <QStyleOptionButton>
  3. static inline uint qt_intensity(uint r, uint g, uint b)
  4. {
  5. /* 30% red, 59% green, 11% blue */
  6. return (77 * r + 150 * g + 28 * b) / 255;
  7. }
  8. /* The constants in the default QT styles don't dim the icons enough in
  9. * disabled mode
  10. *
  11. * https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html#6429
  12. */
  13. QPixmap OBSProxyStyle::generatedIconPixmap(QIcon::Mode iconMode,
  14. const QPixmap &pixmap,
  15. const QStyleOption *option) const
  16. {
  17. if (iconMode == QIcon::Disabled) {
  18. QImage im =
  19. pixmap.toImage().convertToFormat(QImage::Format_ARGB32);
  20. /* Create a colortable based on the background
  21. * (black -> bg -> white) */
  22. QColor bg = option->palette.color(QPalette::Disabled,
  23. QPalette::Window);
  24. int red = bg.red();
  25. int green = bg.green();
  26. int blue = bg.blue();
  27. uchar reds[256], greens[256], blues[256];
  28. for (int i = 0; i < 128; ++i) {
  29. reds[i] = uchar((red * (i << 1)) >> 8);
  30. greens[i] = uchar((green * (i << 1)) >> 8);
  31. blues[i] = uchar((blue * (i << 1)) >> 8);
  32. }
  33. for (int i = 0; i < 128; ++i) {
  34. reds[i + 128] = uchar(qMin(red + (i << 1), 255));
  35. greens[i + 128] = uchar(qMin(green + (i << 1), 255));
  36. blues[i + 128] = uchar(qMin(blue + (i << 1), 255));
  37. }
  38. /* High intensity colors needs dark shifting in the color
  39. * table, while low intensity colors needs light shifting. This
  40. * is to increase the perceived contrast. */
  41. int intensity = qt_intensity(red, green, blue);
  42. const int factor = 191;
  43. if ((red - factor > green && red - factor > blue) ||
  44. (green - factor > red && green - factor > blue) ||
  45. (blue - factor > red && blue - factor > green))
  46. qMin(255, intensity + 20);
  47. else if (intensity <= 128)
  48. intensity += 100;
  49. for (int y = 0; y < im.height(); ++y) {
  50. QRgb *scanLine = (QRgb *)im.scanLine(y);
  51. for (int x = 0; x < im.width(); ++x) {
  52. QRgb pixel = *scanLine;
  53. /* Calculate color table index, taking
  54. * intensity adjustment and a magic offset into
  55. * account. */
  56. uint ci = uint(qGray(pixel) / 3 +
  57. (130 - intensity / 3));
  58. *scanLine = qRgba(reds[ci], greens[ci],
  59. blues[ci], qAlpha(pixel));
  60. ++scanLine;
  61. }
  62. }
  63. return QPixmap::fromImage(im);
  64. }
  65. return QProxyStyle::generatedIconPixmap(iconMode, pixmap, option);
  66. }