pixmapsUtils.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // This file is part of the AppIndicator/KStatusNotifierItem GNOME Shell extension
  2. //
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. export function argbToRgba(src) {
  17. const dest = new Uint8Array(src.length);
  18. for (let j = 0; j < src.length; j += 4) {
  19. const srcAlpha = src[j];
  20. dest[j] = src[j + 1]; /* red */
  21. dest[j + 1] = src[j + 2]; /* green */
  22. dest[j + 2] = src[j + 3]; /* blue */
  23. dest[j + 3] = srcAlpha; /* alpha */
  24. }
  25. return dest;
  26. }
  27. export function getBestPixmap(pixmapsVariant, preferredSize) {
  28. if (!pixmapsVariant)
  29. throw new TypeError('null pixmapsVariant');
  30. const pixmapsVariantsArray = new Array(pixmapsVariant.n_children());
  31. if (!pixmapsVariantsArray.length)
  32. throw TypeError('Empty Icon found');
  33. for (let i = 0; i < pixmapsVariantsArray.length; ++i)
  34. pixmapsVariantsArray[i] = pixmapsVariant.get_child_value(i);
  35. const pixmapsSizedArray = pixmapsVariantsArray.map((pixmapVariant, index) => ({
  36. width: pixmapVariant.get_child_value(0).unpack(),
  37. height: pixmapVariant.get_child_value(1).unpack(),
  38. index,
  39. }));
  40. const sortedIconPixmapArray = pixmapsSizedArray.sort(
  41. ({width: widthA, height: heightA}, {width: widthB, height: heightB}) => {
  42. const areaA = widthA * heightA;
  43. const areaB = widthB * heightB;
  44. return areaA - areaB;
  45. });
  46. // we prefer any pixmap that is equal or bigger than our requested size
  47. const qualifiedIconPixmapArray = sortedIconPixmapArray.filter(({width, height}) =>
  48. width >= preferredSize && height >= preferredSize);
  49. const {width, height, index} = qualifiedIconPixmapArray.length > 0
  50. ? qualifiedIconPixmapArray[0] : sortedIconPixmapArray.pop();
  51. const pixmapVariant = pixmapsVariantsArray[index].get_child_value(2);
  52. const rowStride = width * 4; // hopefully this is correct
  53. return {pixmapVariant, width, height, rowStride};
  54. }