obs-proxy-style.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 OBSContextBarProxyStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap,
  14. const QStyleOption *option) const
  15. {
  16. if (iconMode == QIcon::Disabled) {
  17. QImage im = pixmap.toImage().convertToFormat(QImage::Format_ARGB32);
  18. /* Create a colortable based on the background
  19. * (black -> bg -> white) */
  20. QColor bg = option->palette.color(QPalette::Disabled, QPalette::Window);
  21. int red = bg.red();
  22. int green = bg.green();
  23. int blue = bg.blue();
  24. uchar reds[256], greens[256], blues[256];
  25. for (int i = 0; i < 128; ++i) {
  26. reds[i] = uchar((red * (i << 1)) >> 8);
  27. greens[i] = uchar((green * (i << 1)) >> 8);
  28. blues[i] = uchar((blue * (i << 1)) >> 8);
  29. }
  30. for (int i = 0; i < 128; ++i) {
  31. reds[i + 128] = uchar(qMin(red + (i << 1), 255));
  32. greens[i + 128] = uchar(qMin(green + (i << 1), 255));
  33. blues[i + 128] = uchar(qMin(blue + (i << 1), 255));
  34. }
  35. /* High intensity colors needs dark shifting in the color
  36. * table, while low intensity colors needs light shifting. This
  37. * is to increase the perceived contrast. */
  38. int intensity = qt_intensity(red, green, blue);
  39. const int factor = 191;
  40. if ((red - factor > green && red - factor > blue) || (green - factor > red && green - factor > blue) ||
  41. (blue - factor > red && blue - factor > green))
  42. qMin(255, intensity + 20);
  43. else if (intensity <= 128)
  44. intensity += 100;
  45. for (int y = 0; y < im.height(); ++y) {
  46. QRgb *scanLine = (QRgb *)im.scanLine(y);
  47. for (int x = 0; x < im.width(); ++x) {
  48. QRgb pixel = *scanLine;
  49. /* Calculate color table index, taking
  50. * intensity adjustment and a magic offset into
  51. * account. */
  52. uint ci = uint(qGray(pixel) / 3 + (130 - intensity / 3));
  53. *scanLine = qRgba(reds[ci], greens[ci], blues[ci], qAlpha(pixel));
  54. ++scanLine;
  55. }
  56. }
  57. return QPixmap::fromImage(im);
  58. }
  59. return QProxyStyle::generatedIconPixmap(iconMode, pixmap, option);
  60. }
  61. int OBSProxyStyle::styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget,
  62. QStyleHintReturn *returnData) const
  63. {
  64. if (hint == SH_ComboBox_AllowWheelScrolling)
  65. return 0;
  66. #ifdef __APPLE__
  67. if (hint == SH_ComboBox_UseNativePopup)
  68. return 1;
  69. #endif
  70. return QProxyStyle::styleHint(hint, option, widget, returnData);
  71. }