rgb2xyz.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "core/util/rgb2xyz.h"
  24. #include "math/mMatrix.h"
  25. namespace ConvertRGB
  26. {
  27. // http://www.w3.org/Graphics/Color/sRGB
  28. static const F32 scRGB2XYZ[] =
  29. {
  30. 0.4124f, 0.3576f, 0.1805f, 0.0f,
  31. 0.2126f, 0.7152f, 0.0722f, 0.0f,
  32. 0.0193f, 0.1192f, 0.9505f, 0.0f,
  33. 0.0f, 0.0f, 0.0f, 1.0f,
  34. };
  35. static const F32 scXYZ2RGB[] =
  36. {
  37. 3.2410f, -1.5374f, -0.4986f, 0.0f,
  38. -0.9692f, 1.8760f, 0.0416f, 0.0f,
  39. 0.0556f, -0.2040f, 1.0570f, 0.0f,
  40. 0.0f, 0.0f, 0.0f, 1.0f,
  41. };
  42. LinearColorF toXYZ( const LinearColorF &rgbColor )
  43. {
  44. const MatrixF &rgb2xyz = *((MatrixF *)scRGB2XYZ);
  45. LinearColorF retColor = rgbColor;
  46. rgb2xyz.mul( *(Point4F *)&retColor );
  47. return retColor;
  48. }
  49. LinearColorF fromXYZ( const LinearColorF &xyzColor )
  50. {
  51. const MatrixF &xyz2rgb = *((MatrixF *)scXYZ2RGB);
  52. LinearColorF retColor = xyzColor;
  53. xyz2rgb.mul( *(Point4F *)&retColor );
  54. return retColor;
  55. }
  56. }