You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

118 lines
3.0 KiB

  1. // sixel.c (part of mintty)
  2. // this function is derived from a part of graphics.c
  3. // in Xterm pl#310 originally written by Ross Combs.
  4. //
  5. // Copyright 2013,2014 by Ross Combs
  6. //
  7. // All Rights Reserved
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a
  10. // copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included
  18. // in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  21. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. // IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
  24. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. // Except as contained in this notice, the name(s) of the above copyright
  29. // holders shall not be used in advertising or otherwise to promote the
  30. // sale, use or other dealings in this Software without prior written
  31. // authorization.
  32. #include "sixel-hls.h"
  33. #define SIXEL_RGB(r, g, b) (((r) << 16) + ((g) << 8) + (b))
  34. int
  35. hls_to_rgb(int hue, int lum, int sat)
  36. {
  37. double hs = (hue + 240) % 360;
  38. double hv = hs / 360.0;
  39. double lv = lum / 100.0;
  40. double sv = sat / 100.0;
  41. double c, x, m, c2;
  42. double r1, g1, b1;
  43. int r, g, b;
  44. int hpi;
  45. if (sat == 0) {
  46. r = g = b = lum * 255 / 100;
  47. return SIXEL_RGB(r, g, b);
  48. }
  49. if ((c2 = ((2.0 * lv) - 1.0)) < 0.0) {
  50. c2 = -c2;
  51. }
  52. c = (1.0 - c2) * sv;
  53. hpi = (int) (hv * 6.0);
  54. x = (hpi & 1) ? c : 0.0;
  55. m = lv - 0.5 * c;
  56. switch (hpi) {
  57. case 0:
  58. r1 = c;
  59. g1 = x;
  60. b1 = 0.0;
  61. break;
  62. case 1:
  63. r1 = x;
  64. g1 = c;
  65. b1 = 0.0;
  66. break;
  67. case 2:
  68. r1 = 0.0;
  69. g1 = c;
  70. b1 = x;
  71. break;
  72. case 3:
  73. r1 = 0.0;
  74. g1 = x;
  75. b1 = c;
  76. break;
  77. case 4:
  78. r1 = x;
  79. g1 = 0.0;
  80. b1 = c;
  81. break;
  82. case 5:
  83. r1 = c;
  84. g1 = 0.0;
  85. b1 = x;
  86. break;
  87. default:
  88. return SIXEL_RGB(255, 255, 255);
  89. }
  90. r = (int) ((r1 + m) * 100.0 + 0.5);
  91. g = (int) ((g1 + m) * 100.0 + 0.5);
  92. b = (int) ((b1 + m) * 100.0 + 0.5);
  93. if (r < 0) {
  94. r = 0;
  95. } else if (r > 100) {
  96. r = 100;
  97. }
  98. if (g < 0) {
  99. g = 0;
  100. } else if (g > 100) {
  101. g = 100;
  102. }
  103. if (b < 0) {
  104. b = 0;
  105. } else if (b > 100) {
  106. b = 100;
  107. }
  108. return SIXEL_RGB(r * 255 / 100, g * 255 / 100, b * 255 / 100);
  109. }