hsl.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright (c) 2012 Rodrigo Moraes. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are
  5. met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above
  9. copyright notice, this list of conditions and the following disclaimer
  10. in the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of Google Inc. nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package excelize
  28. import (
  29. "image/color"
  30. "math"
  31. )
  32. // HSLModel converts any color.Color to a HSL color.
  33. var HSLModel = color.ModelFunc(hslModel)
  34. // HSL represents a cylindrical coordinate of points in an RGB color model.
  35. //
  36. // Values are in the range 0 to 1.
  37. type HSL struct {
  38. H, S, L float64
  39. }
  40. // RGBA returns the alpha-premultiplied red, green, blue and alpha values
  41. // for the HSL.
  42. func (c HSL) RGBA() (uint32, uint32, uint32, uint32) {
  43. r, g, b := HSLToRGB(c.H, c.S, c.L)
  44. return uint32(r) * 0x101, uint32(g) * 0x101, uint32(b) * 0x101, 0xffff
  45. }
  46. // hslModel converts a color.Color to HSL.
  47. func hslModel(c color.Color) color.Color {
  48. if _, ok := c.(HSL); ok {
  49. return c
  50. }
  51. r, g, b, _ := c.RGBA()
  52. h, s, l := RGBToHSL(uint8(r>>8), uint8(g>>8), uint8(b>>8))
  53. return HSL{h, s, l}
  54. }
  55. // RGBToHSL converts an RGB triple to a HSL triple.
  56. func RGBToHSL(r, g, b uint8) (h, s, l float64) {
  57. fR := float64(r) / 255
  58. fG := float64(g) / 255
  59. fB := float64(b) / 255
  60. max := math.Max(math.Max(fR, fG), fB)
  61. min := math.Min(math.Min(fR, fG), fB)
  62. l = (max + min) / 2
  63. if max == min {
  64. // Achromatic.
  65. h, s = 0, 0
  66. } else {
  67. // Chromatic.
  68. d := max - min
  69. if l > 0.5 {
  70. s = d / (2.0 - max - min)
  71. } else {
  72. s = d / (max + min)
  73. }
  74. switch max {
  75. case fR:
  76. h = (fG - fB) / d
  77. if fG < fB {
  78. h += 6
  79. }
  80. case fG:
  81. h = (fB-fR)/d + 2
  82. case fB:
  83. h = (fR-fG)/d + 4
  84. }
  85. h /= 6
  86. }
  87. return
  88. }
  89. // HSLToRGB converts an HSL triple to a RGB triple.
  90. func HSLToRGB(h, s, l float64) (r, g, b uint8) {
  91. var fR, fG, fB float64
  92. if s == 0 {
  93. fR, fG, fB = l, l, l
  94. } else {
  95. var q float64
  96. if l < 0.5 {
  97. q = l * (1 + s)
  98. } else {
  99. q = l + s - s*l
  100. }
  101. p := 2*l - q
  102. fR = hueToRGB(p, q, h+1.0/3)
  103. fG = hueToRGB(p, q, h)
  104. fB = hueToRGB(p, q, h-1.0/3)
  105. }
  106. r = uint8((fR * 255) + 0.5)
  107. g = uint8((fG * 255) + 0.5)
  108. b = uint8((fB * 255) + 0.5)
  109. return
  110. }
  111. // hueToRGB is a helper function for HSLToRGB.
  112. func hueToRGB(p, q, t float64) float64 {
  113. if t < 0 {
  114. t++
  115. }
  116. if t > 1 {
  117. t--
  118. }
  119. if t < 1.0/6 {
  120. return p + (q-p)*6*t
  121. }
  122. if t < 0.5 {
  123. return q
  124. }
  125. if t < 2.0/3 {
  126. return p + (q-p)*(2.0/3-t)*6
  127. }
  128. return p
  129. }