hsl.go 3.8 KB

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