colors.agda 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. {-- colors.agda - color app
  2. -- Copyright (C) 2019 caryoscelus
  3. --
  4. -- This program is free software: you can redistribute it and/or modify
  5. -- it under the terms of the GNU General Public License as published by
  6. -- the Free Software Foundation, either version 3 of the License, or
  7. -- (at your option) any later version.
  8. --
  9. -- This program is distributed in the hope that it will be useful,
  10. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. -- GNU General Public License for more details.
  13. --
  14. -- You should have received a copy of the GNU General Public License
  15. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. --}
  17. module colors where
  18. open import Data.Empty
  19. open import Data.Unit
  20. open import Data.Bool
  21. open import Data.Product
  22. open import Data.Nat
  23. open import Data.Nat.Properties
  24. import Data.Integer as ℤ
  25. open import Data.List
  26. import Data.List as L
  27. -- import IO.Primitive as Prim
  28. import IO
  29. open import Function
  30. open import Relation.Binary.PropositionalEquality
  31. open import Relation.Nullary
  32. open import Hask
  33. open import Util
  34. open import NanoLens
  35. open import GLApp
  36. import GLFW
  37. open GLFW.Types
  38. record HSV (C : Set) : Set where
  39. constructor hsv
  40. field
  41. hue saturation value : C
  42. open HSV
  43. record RGB (C : Set) : Set where
  44. constructor rgb
  45. field
  46. red green blue : C
  47. open RGB
  48. record Fin256 : Set where
  49. constructor fin
  50. field
  51. n : ℕ
  52. ok : n <′ 256
  53. fin0 = fin 0 (≤⇒≤′ (s≤s z≤n))
  54. finMax = fin 255 ≤′-refl
  55. 256-neg : Fin256 → Fin256
  56. 256-neg (fin n _) =
  57. fin (255 ∸ n) (≤⇒≤′ (s≤s (n∸m≤n n 255)))
  58. hsvRed : HSV Fin256
  59. hue hsvRed = fin0
  60. saturation hsvRed = finMax
  61. value hsvRed = finMax
  62. record ColorsApp : Set where
  63. field
  64. hsvColor : HSV Fin256 -- bah
  65. cursor : Point
  66. updated : Bool
  67. open ColorsApp
  68. emptyApp : ColorsApp
  69. emptyApp = record
  70. { hsvColor = hsvRed
  71. ; cursor = v2 (ℤ.+ 0) (ℤ.+ 0)
  72. ; updated = true
  73. }
  74. hueToRgb : Fin256 → RGB Fin256
  75. hueToRgb x = rgb x finMax finMax -- rgb finMax fin0 fin0 -- TODO
  76. [ℕ_/_] = ℕratioToFloat
  77. [ℤ_/_] = ℤratioToFloat
  78. rgb256⇒float : RGB Fin256 → V3 Float
  79. rgb256⇒float c = v3
  80. [ℕ Fin256.n (red c) / 255 ]
  81. [ℕ Fin256.n (green c) / 255 ]
  82. [ℕ Fin256.n (blue c) / 255 ]
  83. renderColor : ColorsApp → Triangles
  84. renderColor app =
  85. let
  86. col = hsvColor app
  87. col′ = rgb (hue col) (saturation col) (value col)
  88. ↖ = v2 [ℤ ℤ.- (ℤ.+ 1) / ℤ.+ 1 ] [ℕ 1 / 1 ]
  89. ↗ = v2 [ℕ 0 / 1 ] [ℕ 1 / 1 ]
  90. ↙ = v2 [ℤ ℤ.- (ℤ.+ 1) / ℤ.+ 1 ] [ℕ 0 / 1 ]
  91. ↘ = v2 [ℕ 0 / 1 ] [ℕ 0 / 1 ]
  92. in
  93. L.map (λ xy → rgbPoint xy (rgb256⇒float col′))
  94. (↖ ∷ ↗ ∷ ↙ ∷ ↗ ∷ ↙ ∷ ↘ ∷ [])
  95. renderSVRect : ColorsApp → Triangles
  96. renderSVRect app =
  97. let
  98. col = hueToRgb ∘ hue ∘ hsvColor $ app
  99. black : RGB Fin256
  100. black = rgb fin0 fin0 fin0
  101. black′ = rgb fin0 finMax fin0
  102. white : RGB Fin256
  103. white = rgb fin0 fin0 finMax
  104. ↙ = (v2 [ℕ 0 / 1 ] [ℕ 0 / 1 ] , black′)
  105. ↘ = (v2 [ℕ 1 / 1 ] [ℕ 0 / 1 ] , black)
  106. ↖ = (v2 [ℕ 0 / 1 ] [ℕ 1 / 1 ] , col)
  107. ↗ = (v2 [ℕ 1 / 1 ] [ℕ 1 / 1 ] , white)
  108. in
  109. L.map (λ { ( xy , color ) → rgbPoint xy (rgb256⇒float color)})
  110. (↖ ∷ ↗ ∷ ↙ ∷ ↗ ∷ ↙ ∷ ↘ ∷ [])
  111. render : ColorsApp → Triangles
  112. render app = renderSVRect app ++ renderColor app
  113. ℕ⇒256 : ℕ → Fin256
  114. ℕ⇒256 x with x <? 256
  115. ... | yes p = fin x (≤⇒≤′ p)
  116. ... | no _ = fin 255 ≤′-refl
  117. Coord⇒256 : Coord → Fin256
  118. Coord⇒256 = ℕ⇒256 ∘ λ { (ℤ.+_ n) → n ; (ℤ.-[1+_] n) → 0 }
  119. mouseCallback : GLFW.MouseCallback ColorsApp
  120. mouseCallback button MouseButtonState'Pressed _ app =
  121. ( set ፦[ updated ] true
  122. ⟫ set (፦[ hsvColor ] ፦⟫ ፦[ saturation ]) (256-neg ∘ Coord⇒256 ∘ V2.x ∘ cursor $ app)
  123. ⟫ set (፦[ hsvColor ] ፦⟫ ፦[ value ]) (Coord⇒256 ∘ V2.y ∘ cursor $ app)
  124. ) app
  125. mouseCallback button MouseButtonState'Released _ = id
  126. cursorCallback : GLFW.CursorCallback ColorsApp
  127. cursorCallback x y = set ፦[ cursor ] (screenToGl wh wh x y)
  128. drawColorsApp : DrawApp ColorsApp
  129. drawColorsApp = record
  130. { empty = emptyApp
  131. ; render = λ x → record { newState = x ; result = render x }
  132. ; isDirty = λ _ _ → true
  133. ; frameCount = λ _ → 1
  134. ; nowFrame = λ _ → 0
  135. ; dontClearTexture = set ፦[ updated ] false
  136. ; getNeedToClearTexture = updated
  137. ; mouseCallback = GLFW.mouseCallbackWrap mouseCallback
  138. ; cursorCallback = GLFW.cursorCallbackWrap cursorCallback
  139. ; keyCallback = GLFW.keyCallbackWrap λ key scancode state mods → id
  140. }
  141. main = run ∘ lift ∘ everything $ drawColorsApp
  142. where open IO