hsbramp.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: hsbramp.c /main/3 1995/11/02 16:07:23 rswiston $ */
  24. /*
  25. */
  26. /* *
  27. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  28. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  29. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  30. * (c) Copyright 1993, 1994 Novell, Inc. *
  31. */
  32. /*-
  33. * hsbramp.c - Create an HSB ramp.
  34. *
  35. * Copyright (c) 1991 by Patrick J. Naughton.
  36. *
  37. * See dtscreen.c for copying information.
  38. *
  39. * Revision History:
  40. * 29-Jul-90: renamed hsbramp.c from HSBmap.c
  41. * minor optimizations.
  42. * 01-Sep-88: Written.
  43. */
  44. #include <sys/types.h>
  45. #include <math.h>
  46. void
  47. hsb2rgb(double H, double S, double B, u_char *r, u_char *g, u_char *b)
  48. {
  49. int i;
  50. double f;
  51. double bb;
  52. u_char p;
  53. u_char q;
  54. u_char t;
  55. H -= floor(H); /* remove anything over 1 */
  56. H *= 6.0;
  57. i = floor(H); /* 0..5 */
  58. f = H - (float) i; /* f = fractional part of H */
  59. bb = 255.0 * B;
  60. p = (u_char) (bb * (1.0 - S));
  61. q = (u_char) (bb * (1.0 - (S * f)));
  62. t = (u_char) (bb * (1.0 - (S * (1.0 - f))));
  63. switch (i) {
  64. case 0:
  65. *r = (u_char) bb;
  66. *g = t;
  67. *b = p;
  68. break;
  69. case 1:
  70. *r = q;
  71. *g = (u_char) bb;
  72. *b = p;
  73. break;
  74. case 2:
  75. *r = p;
  76. *g = (u_char) bb;
  77. *b = t;
  78. break;
  79. case 3:
  80. *r = p;
  81. *g = q;
  82. *b = (u_char) bb;
  83. break;
  84. case 4:
  85. *r = t;
  86. *g = p;
  87. *b = (u_char) bb;
  88. break;
  89. case 5:
  90. *r = (u_char) bb;
  91. *g = p;
  92. *b = q;
  93. break;
  94. }
  95. }
  96. /*
  97. * Input is two points in HSB color space and a count
  98. * of how many discreet rgb space values the caller wants.
  99. *
  100. * Output is that many rgb triples which describe a linear
  101. * interpolate ramp between the two input colors.
  102. */
  103. void
  104. hsbramp(double h1, double s1, double b1,
  105. double h2, double s2, double b2,
  106. int count,
  107. u_char *red, u_char *green, u_char *blue)
  108. {
  109. double dh;
  110. double ds;
  111. double db;
  112. dh = (h2 - h1) / count;
  113. ds = (s2 - s1) / count;
  114. db = (b2 - b1) / count;
  115. while (count--) {
  116. hsb2rgb(h1, s1, b1, red++, green++, blue++);
  117. h1 += dh;
  118. s1 += ds;
  119. b1 += db;
  120. }
  121. }