sRGB.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*-
  2. * sRGB.h
  3. *
  4. * Last changed in libpng 1.6.0 [February 14, 2013]
  5. *
  6. * COPYRIGHT: Written by John Cunningham Bowler, 2013.
  7. * To the extent possible under law, the author has waived all copyright and
  8. * related or neighboring rights to this work. This work is published from:
  9. * United States.
  10. *
  11. * Utility file; not actually a header, this contains definitions of sRGB
  12. * calculation functions for inclusion in those test programs that need them.
  13. *
  14. * All routines take and return a floating point value in the range
  15. * 0 to 1.0, doing a calculation according to the sRGB specification
  16. * (in fact the source of the numbers is the wikipedia article at
  17. * http://en.wikipedia.org/wiki/SRGB).
  18. */
  19. static double
  20. sRGB_from_linear(double l)
  21. {
  22. if (l <= 0.0031308)
  23. l *= 12.92;
  24. else
  25. l = 1.055 * pow(l, 1/2.4) - 0.055;
  26. return l;
  27. }
  28. static double
  29. linear_from_sRGB(double s)
  30. {
  31. if (s <= 0.04045)
  32. return s / 12.92;
  33. else
  34. return pow((s+0.055)/1.055, 2.4);
  35. }
  36. static double
  37. YfromRGB(double r, double g, double b)
  38. {
  39. /* Use the sRGB (rounded) coefficients for Rlinear, Glinear, Blinear to get
  40. * the CIE Y value (also linear).
  41. */
  42. return 0.2126 * r + 0.7152 * g + 0.0722 * b;
  43. }