obs-proxy-style.cpp 2.6 KB

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