colorscale.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2022 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ebiten
  15. import (
  16. "fmt"
  17. "image/color"
  18. )
  19. // ColorScale represents a scale of RGBA color.
  20. // ColorScale is intended to be applied to a premultiplied-alpha color value.
  21. //
  22. // The initial (zero) value of ColorScale is an identity scale (1, 1, 1, 1).
  23. type ColorScale struct {
  24. // These values are adjusted by -1 from the actual values.
  25. // It's because the initial value should be 1 instead of 0.
  26. r_1, g_1, b_1, a_1 float32
  27. }
  28. // String returns a string representing the color scale.
  29. func (c *ColorScale) String() string {
  30. return fmt.Sprintf("(%f,%f,%f,%f)", c.r_1+1, c.g_1+1, c.b_1+1, c.a_1+1)
  31. }
  32. // Reset resets the ColorScale as identity.
  33. func (c *ColorScale) Reset() {
  34. c.r_1 = 0
  35. c.g_1 = 0
  36. c.b_1 = 0
  37. c.a_1 = 0
  38. }
  39. // R returns the red scale.
  40. func (c *ColorScale) R() float32 {
  41. return c.r_1 + 1
  42. }
  43. // G returns the green scale.
  44. func (c *ColorScale) G() float32 {
  45. return c.g_1 + 1
  46. }
  47. // B returns the blue scale.
  48. func (c *ColorScale) B() float32 {
  49. return c.b_1 + 1
  50. }
  51. // A returns the alpha scale.
  52. func (c *ColorScale) A() float32 {
  53. return c.a_1 + 1
  54. }
  55. func (c *ColorScale) elements() (float32, float32, float32, float32) {
  56. return c.r_1 + 1, c.g_1 + 1, c.b_1 + 1, c.a_1 + 1
  57. }
  58. // SetR overwrites the current red value with r.
  59. func (c *ColorScale) SetR(r float32) {
  60. c.r_1 = r - 1
  61. }
  62. // SetG overwrites the current green value with g.
  63. func (c *ColorScale) SetG(g float32) {
  64. c.g_1 = g - 1
  65. }
  66. // SetB overwrites the current blue value with b.
  67. func (c *ColorScale) SetB(b float32) {
  68. c.b_1 = b - 1
  69. }
  70. // SetA overwrites the current alpha value with a.
  71. func (c *ColorScale) SetA(a float32) {
  72. c.a_1 = a - 1
  73. }
  74. // Scale multiplies the given values to the current scale.
  75. //
  76. // Scale is slightly different from colorm.ColorM's Scale in terms of alphas.
  77. // ColorScale is applied to premultiplied-alpha colors, while colorm.ColorM is applied to straight-alpha colors.
  78. // Thus, ColorM.Scale(r, g, b, a) equals to ColorScale.Scale(r*a, g*a, b*a, a).
  79. func (c *ColorScale) Scale(r, g, b, a float32) {
  80. c.r_1 = (c.r_1+1)*r - 1
  81. c.g_1 = (c.g_1+1)*g - 1
  82. c.b_1 = (c.b_1+1)*b - 1
  83. c.a_1 = (c.a_1+1)*a - 1
  84. }
  85. // ScaleAlpha multiplies the given alpha value to the current scale.
  86. func (c *ColorScale) ScaleAlpha(a float32) {
  87. c.r_1 = (c.r_1+1)*a - 1
  88. c.g_1 = (c.g_1+1)*a - 1
  89. c.b_1 = (c.b_1+1)*a - 1
  90. c.a_1 = (c.a_1+1)*a - 1
  91. }
  92. // ScaleWithColor multiplies the given color values to the current scale.
  93. func (c *ColorScale) ScaleWithColor(clr color.Color) {
  94. cr, cg, cb, ca := clr.RGBA()
  95. c.Scale(float32(cr)/0xffff, float32(cg)/0xffff, float32(cb)/0xffff, float32(ca)/0xffff)
  96. }
  97. // ScaleWithColorScale multiplies the given color scale values to the current scale.
  98. func (c *ColorScale) ScaleWithColorScale(colorScale ColorScale) {
  99. c.r_1 = (c.r_1+1)*(colorScale.r_1+1) - 1
  100. c.g_1 = (c.g_1+1)*(colorScale.g_1+1) - 1
  101. c.b_1 = (c.b_1+1)*(colorScale.b_1+1) - 1
  102. c.a_1 = (c.a_1+1)*(colorScale.a_1+1) - 1
  103. }
  104. func (c *ColorScale) apply(r, g, b, a float32) (float32, float32, float32, float32) {
  105. return (c.r_1 + 1) * r, (c.g_1 + 1) * g, (c.b_1 + 1) * b, (c.a_1 + 1) * a
  106. }