Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. package o1.hofuncs
  2.  
  3. // This program is introduced in Chapter 5.5.
  4.  
  5. import o1._
  6.  
  7. object Task1 extends App {
  8.  
  9. def toNaiveGray(color: Color) = {
  10. val luminance = (color.red + color.blue + color.green) / 3
  11. Color(luminance, luminance, luminance)
  12. }
  13.  
  14. // The function toNaiveGray above takes in a color and returns a grayscale ("colorless")
  15. // version of it, that is, a shade of gray between white and black. As you can see from
  16. // the implementation, the R, G, and B components of a shade of gray are equal.
  17. //
  18. // toNaiveGray computes the shade of gray with a simple average: each of the three
  19. // components has an equal weight of 1/3. This works fairly okay, as you can tell by
  20. // running the program.
  21. //
  22. // However, the human eye is most sensitive to green light and much less sensitive to blue.
  23. // Therefore, to produce a grayscale image that humans perceive as equally luminous (bright)
  24. // as the original, we need to give different weights to the components of the original color:
  25. // the green component should have the greatest weight and the blue component the least.
  26. // Here is a common formula (see https://en.wikipedia.org/wiki/Grayscale):
  27. // luminance = 0.2126*R + 0.7152*G + 0.0722*B
  28. // Such a grayscale filter that seeks to preserve perceived luminosity is called
  29. // "colorimetric".
  30. //
  31. // Your task is to modify this program by adding a function toColorimetricGray, which works
  32. // like toNaiveGray except that it uses the above formula to return a more realistic shade
  33. // of gray. Use the new function below (instead of toNaiveGray) to compute grayPic.
  34. //
  35. // Note: You can pass in Doubles as you construct a Color; they will be rounded to the
  36. // nearest Int.
  37.  
  38. val originalPic = Pic("kid.png")
  39. val grayPic = originalPic.transformColors(toColorimetricGray)
  40.  
  41.  
  42. originalPic.leftOf(grayPic).show()
  43.  
  44. def toColorimetricGray(color: Color) = {
  45.  
  46. val luminance = ((color.red) * 0.2126 + (color.blue * 0.0722) + (color.green * 0.7152))
  47. Color(luminance, luminance, luminance)
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement