ImagingColors.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. {
  2. $Id$
  3. Vampyre Imaging Library
  4. by Marek Mauder
  5. http://imaginglib.sourceforge.net
  6. The contents of this file are used with permission, subject to the Mozilla
  7. Public License Version 1.1 (the "License"); you may not use this file except
  8. in compliance with the License. You may obtain a copy of the License at
  9. http://www.mozilla.org/MPL/MPL-1.1.html
  10. Software distributed under the License is distributed on an "AS IS" basis,
  11. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  12. the specific language governing rights and limitations under the License.
  13. Alternatively, the contents of this file may be used under the terms of the
  14. GNU Lesser General Public License (the "LGPL License"), in which case the
  15. provisions of the LGPL License are applicable instead of those above.
  16. If you wish to allow use of your version of this file only under the terms
  17. of the LGPL License and not to allow others to use your version of this file
  18. under the MPL, indicate your decision by deleting the provisions above and
  19. replace them with the notice and other provisions required by the LGPL
  20. License. If you do not delete the provisions above, a recipient may use
  21. your version of this file under either the MPL or the LGPL License.
  22. For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
  23. }
  24. { This unit contains functions for manipulating and converting color values.}
  25. unit ImagingColors;
  26. interface
  27. {$I ImagingOptions.inc}
  28. uses
  29. SysUtils, ImagingTypes, ImagingUtility;
  30. { Converts RGB color to YUV.}
  31. procedure RGBToYUV(R, G, B: Byte; var Y, U, V: Byte);
  32. { Converts YIV to RGB color.}
  33. procedure YUVToRGB(Y, U, V: Byte; var R, G, B: Byte);
  34. { Converts RGB color to YCbCr as used in JPEG.}
  35. procedure RGBToYCbCr(R, G, B: Byte; var Y, Cb, Cr: Byte);
  36. { Converts YCbCr as used in JPEG to RGB color.}
  37. procedure YCbCrToRGB(Y, Cb, Cr: Byte; var R, G, B: Byte);
  38. { Converts RGB color to YCbCr as used in JPEG.}
  39. procedure RGBToYCbCr16(R, G, B: Word; var Y, Cb, Cr: Word);
  40. { Converts YCbCr as used in JPEG to RGB color.}
  41. procedure YCbCrToRGB16(Y, Cb, Cr: Word; var R, G, B: Word);
  42. { Converts RGB color to CMY.}
  43. procedure RGBToCMY(R, G, B: Byte; var C, M, Y: Byte);
  44. { Converts CMY to RGB color.}
  45. procedure CMYToRGB(C, M, Y: Byte; var R, G, B: Byte);
  46. { Converts RGB color to CMYK.}
  47. procedure RGBToCMYK(R, G, B: Byte; var C, M, Y, K: Byte);
  48. { Converts CMYK to RGB color.}
  49. procedure CMYKToRGB(C, M, Y, K: Byte; var R, G, B: Byte);
  50. implementation
  51. procedure RGBToYUV(R, G, B: Byte; var Y, U, V: Byte);
  52. begin
  53. Y := ClampToByte(Round( 0.257 * R + 0.504 * G + 0.098 * B) + 16);
  54. V := ClampToByte(Round( 0.439 * R - 0.368 * G - 0.071 * B) + 128);
  55. U := ClampToByte(Round(-0.148 * R - 0.291 * G + 0.439 * B) + 128);
  56. end;
  57. procedure YUVToRGB(Y, U, V: Byte; var R, G, B: Byte);
  58. var
  59. CY, CU, CV: LongInt;
  60. begin
  61. CY := Y - 16;
  62. CU := U - 128;
  63. CV := V - 128;
  64. R := ClampToByte(Round(1.164 * CY - 0.002 * CU + 1.596 * CV));
  65. G := ClampToByte(Round(1.164 * CY - 0.391 * CU - 0.813 * CV));
  66. B := ClampToByte(Round(1.164 * CY + 2.018 * CU - 0.001 * CV));
  67. end;
  68. procedure RGBToYCbCr(R, G, B: Byte; var Y, Cb, Cr: Byte);
  69. begin
  70. Y := ClampToByte(Round( 0.29900 * R + 0.58700 * G + 0.11400 * B));
  71. Cb := ClampToByte(Round(-0.16874 * R - 0.33126 * G + 0.50000 * B + 128));
  72. Cr := ClampToByte(Round( 0.50000 * R - 0.41869 * G - 0.08131 * B + 128));
  73. end;
  74. procedure YCbCrToRGB(Y, Cb, Cr: Byte; var R, G, B: Byte);
  75. begin
  76. R := ClampToByte(Round(Y + 1.40200 * (Cr - 128)));
  77. G := ClampToByte(Round(Y - 0.34414 * (Cb - 128) - 0.71414 * (Cr - 128)));
  78. B := ClampToByte(Round(Y + 1.77200 * (Cb - 128)));
  79. end;
  80. procedure RGBToYCbCr16(R, G, B: Word; var Y, Cb, Cr: Word);
  81. begin
  82. Y := ClampToWord(Round( 0.29900 * R + 0.58700 * G + 0.11400 * B));
  83. Cb := ClampToWord(Round(-0.16874 * R - 0.33126 * G + 0.50000 * B + 32768));
  84. Cr := ClampToWord(Round( 0.50000 * R - 0.41869 * G - 0.08131 * B + 32768));
  85. end;
  86. procedure YCbCrToRGB16(Y, Cb, Cr: Word; var R, G, B: Word);
  87. begin
  88. R := ClampToWord(Round(Y + 1.40200 * (Cr - 32768)));
  89. G := ClampToWord(Round(Y - 0.34414 * (Cb - 32768) - 0.71414 * (Cr - 32768)));
  90. B := ClampToWord(Round(Y + 1.77200 * (Cb - 32768)));
  91. end;
  92. procedure RGBToCMY(R, G, B: Byte; var C, M, Y: Byte);
  93. begin
  94. C := 255 - R;
  95. M := 255 - G;
  96. Y := 255 - B;
  97. end;
  98. procedure CMYToRGB(C, M, Y: Byte; var R, G, B: Byte);
  99. begin
  100. R := 255 - C;
  101. G := 255 - M;
  102. B := 255 - Y;
  103. end;
  104. procedure RGBToCMYK(R, G, B: Byte; var C, M, Y, K: Byte);
  105. begin
  106. RGBToCMY(R, G, B, C, M, Y);
  107. K := Min(C, Min(M, Y));
  108. if K > 0 then
  109. begin
  110. C := C - K;
  111. M := M - K;
  112. Y := Y - K;
  113. end;
  114. end;
  115. procedure CMYKToRGB(C, M, Y, K: Byte; var R, G, B: Byte);
  116. begin
  117. if C + K < 255 then R := 255 - (C + K) else R := 0;
  118. if M + K < 255 then G := 255 - (M + K) else G := 0;
  119. if Y + K < 255 then B := 255 - (Y + K) else B := 0;
  120. end;
  121. {
  122. File Notes:
  123. -- TODOS ----------------------------------------------------
  124. - nothing now
  125. -- 0.21 Changes/Bug Fixes -----------------------------------
  126. - Added some color space conversion functions and LUTs
  127. (RGB/YUV/YCrCb/CMY/CMYK).
  128. -- 0.17 Changes/Bug Fixes -----------------------------------
  129. - unit created (empty!)
  130. }
  131. end.