ColorTools.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Open Tracker License
  3. Terms and Conditions
  4. Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. this software and associated documentation files (the "Software"), to deal in
  7. the Software without restriction, including without limitation the rights to
  8. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. of the Software, and to permit persons to whom the Software is furnished to do
  10. so, subject to the following conditions:
  11. The above copyright notice and this permission notice applies to all licensees
  12. and shall be included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  16. BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  17. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
  18. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. Except as contained in this notice, the name of Be Incorporated shall not be
  20. used in advertising or otherwise to promote the sale, use or other dealings in
  21. this Software without prior written authorization from Be Incorporated.
  22. Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
  23. of Be Incorporated in the United States and other countries. Other brand product
  24. names are registered trademarks or trademarks of their respective holders.
  25. All rights reserved.
  26. */
  27. /*******************************************************************************
  28. /
  29. / File: ColorTools.cpp
  30. /
  31. / Description: Additional experimental color manipulation functions.
  32. /
  33. / Copyright 2000, Be Incorporated, All Rights Reserved
  34. /
  35. *******************************************************************************/
  36. #include "ColorTools.h"
  37. #if B_BEOS_VERSION <= B_BEOS_VERSION_MAUI
  38. namespace BExperimental {
  39. #if DEBUG
  40. #define DB_INLINE
  41. #else
  42. #define DB_INLINE inline
  43. #endif
  44. static DB_INLINE void mix_color_func(rgb_color* target, const rgb_color other, uint8 amount)
  45. {
  46. target->red = (uint8)( ((int16(other.red)-int16(target->red))*amount)/255
  47. + target->red );
  48. target->green = (uint8)( ((int16(other.green)-int16(target->green))*amount)/255
  49. + target->green );
  50. target->blue = (uint8)( ((int16(other.blue)-int16(target->blue))*amount)/255
  51. + target->blue );
  52. target->alpha = (uint8)( ((int16(other.alpha)-int16(target->alpha))*amount)/255
  53. + target->alpha );
  54. }
  55. static DB_INLINE void blend_color_func(rgb_color* target, const rgb_color other, uint8 amount)
  56. {
  57. const uint8 alphaMix = (uint8)( ((int16(other.alpha)-int16(255-target->alpha))*amount)/255
  58. + (255-target->alpha) );
  59. target->red = (uint8)( ((int16(other.red)-int16(target->red))*alphaMix)/255
  60. + target->red );
  61. target->green = (uint8)( ((int16(other.green)-int16(target->green))*alphaMix)/255
  62. + target->green );
  63. target->blue = (uint8)( ((int16(other.blue)-int16(target->blue))*alphaMix)/255
  64. + target->blue );
  65. target->alpha = (uint8)( ((int16(other.alpha)-int16(target->alpha))*amount)/255
  66. + target->alpha );
  67. }
  68. static DB_INLINE void disable_color_func(rgb_color* target, const rgb_color background)
  69. {
  70. blend_color_func(target, background, 255-70);
  71. }
  72. // --------------------------------------------------------------------------
  73. rgb_color mix_color(rgb_color color1, rgb_color color2, uint8 amount)
  74. {
  75. mix_color_func(&color1, color2, amount);
  76. return color1;
  77. }
  78. rgb_color blend_color(rgb_color color1, rgb_color color2, uint8 amount)
  79. {
  80. blend_color_func(&color1, color2, amount);
  81. return color1;
  82. }
  83. rgb_color disable_color(rgb_color color, rgb_color background)
  84. {
  85. disable_color_func(&color, background);
  86. return color;
  87. }
  88. }
  89. #endif