iup_color_browser.e 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. class IUP_COLOR_BROWSER
  2. -- Creates an element for selecting a color. The selection is done using a
  3. -- cylindrical projection of the RGB cube. The transformation defines a
  4. -- coordinate color system called HSI, that is still the RGB color space but
  5. -- using cylindrical coordinates.
  6. --
  7. -- H is for Hue, and it is the angle around the RGB cube diagonal starting at
  8. -- red (RGB=255 0 0).
  9. --
  10. -- S is for Saturation, and it is the normal distance from the color to the
  11. -- diagonal, normalized by its maximum value at the specified Hue. This also
  12. -- defines a point at the diagonal used to define I.
  13. --
  14. -- I is for Intensity, and it is the distance from the point defined at the
  15. -- diagonal to black (RGB=0 0 0). I can also be seen as the projection of the
  16. -- color vector onto the diagonal. But I is not linear, see Notes below.
  17. --
  18. -- For a dialog that simply returns the selected color, you can use function
  19. -- IUP_GET_COLOR or IUP_COLOR_DIALOG.
  20. --
  21. -- Notes:
  22. --
  23. -- When the control has the focus the keyboard can be used to change the color
  24. -- value. Use the arrow keys to move the cursor inside the SI triangle, and use
  25. -- Home(0), PageUp, PageDn and End(180) keys to move the cursor inside the Hue
  26. -- circle.
  27. --
  28. -- The Hue in the HSI coordinate system defines a plane that it is a triangle
  29. -- in the RGB cube. But the maximum saturation in this triangle is different
  30. -- for each Hue because of the geometry of the cube. In ColorBrowser this point
  31. -- is fixed at the center of the I axis. So the I axis is not completely
  32. -- linear, it is linear in two parts, one from 0 to 0.5, and another from 0.5
  33. -- to 1.0. Although the selected values are linear specified you can notice
  34. -- that when Hue is changed the gray scale also changes, visually compacting
  35. -- values above or below the I=0.5 line according to the selected Hue.
  36. --
  37. -- This is the same HSI specified in the IM toolkit, except for the non
  38. -- linearity of I. This non linearity were introduced so a simple triangle
  39. -- could be used to represent the SI plane.
  40. inherit
  41. IUP_WIDGET
  42. redefine
  43. execute_change,
  44. execute_drag,
  45. execute_valuechanged
  46. end
  47. IUP_WIDGET_INTERNALS
  48. IUP_WIDGET_ACTIVE
  49. IUP_WIDGET_BGCOLOR
  50. IUP_WIDGET_FONT
  51. IUP_WIDGET_POSITION
  52. IUP_WIDGET_MAXMIN_SIZE
  53. IUP_WIDGET_WID
  54. IUP_WIDGET_TIP
  55. IUP_WIDGET_SIZE
  56. IUP_WIDGET_ZORDER
  57. IUP_WIDGET_VISIBLE
  58. IUP_WIDGET_RASTERSIZE
  59. redefine
  60. set_raster_size
  61. end
  62. IUP_WIDGET_EXPAND
  63. redefine
  64. set_expand
  65. end
  66. create {ANY}
  67. color_browser
  68. feature {ANY}
  69. color_browser
  70. local
  71. a_color_browser: POINTER
  72. do
  73. a_color_browser := int_color_browser
  74. set_widget(a_color_browser)
  75. end
  76. -- Attributes
  77. set_expand (type: STRING)
  78. -- The default value is "False".
  79. do
  80. Precursor (type)
  81. end
  82. set_raster_size (width: INTEGER; height: INTEGER)
  83. -- (non inheritable): the initial size is "181x181".
  84. do
  85. Precursor (width, height)
  86. end
  87. set_automatic_layout
  88. -- Set to allow the automatic layout use smaller values.
  89. do
  90. iup_open.set_attribute_null(Current, "RASTERSIZE")
  91. end
  92. set_value_rgb (red, green, blue: INTEGER)
  93. -- The color value in RGB coordinates and optionally alpha. It is used as
  94. -- the initial value and contains the selected value if the user pressed
  95. -- the Ok button. Format: "R G B A". Each component range from 0 to 255.
  96. do
  97. iup_open.set_attribute(Current, "RGB", rgb_to_string(red,
  98. green,
  99. blue))
  100. end
  101. get_value_rgb: TUPLE[INTEGER, INTEGER, INTEGER]
  102. -- Return the selected value.
  103. do
  104. Result := iup_open.get_rgb(Current, "RGB")
  105. end
  106. set_value_hsi (h, s, i: REAL_64)
  107. -- (non inheritable): the color selected in the control, in the "h s i"
  108. -- format; h, s and i are floating point numbers ranging from 0-360, 0-1
  109. -- and 0-1 respectively.
  110. do
  111. iup_open.set_attribute(Current, "HSI", hsi_real_to_string(h, s, i))
  112. end
  113. get_value_hsi: TUPLE[REAL_64, REAL_64, REAL_64]
  114. -- Return the selected value.
  115. do
  116. Result := iup_open.get_hsi_real(Current, "HSI")
  117. end
  118. -- Callbacks
  119. set_cb_change (act: detachable FUNCTION[TUPLE[IUP_COLOR_BROWSER, INTEGER, INTEGER, INTEGER], STRING])
  120. -- Called when the user releases the left mouse button over the control,
  121. -- defining the selected color.
  122. --
  123. -- ih: identifier of the element that activated the event.
  124. -- red, green, blue: color value.
  125. local
  126. operation: INTEGER
  127. do
  128. cb_change := act
  129. if cb_change /= Void then
  130. operation := 1
  131. else
  132. operation := 0
  133. end
  134. iup_open.set_callback (Current, "CHANGE_CB", "NONEEDED", operation)
  135. end
  136. set_cb_drag (act: detachable FUNCTION[TUPLE[IUP_COLOR_BROWSER, INTEGER, INTEGER, INTEGER], STRING])
  137. -- Called several times while the color is being changed by dragging the
  138. -- mouse over the control.
  139. --
  140. -- ih: identifier of the element that activated the event.
  141. -- red, green, blue: color value.
  142. local
  143. operation: INTEGER
  144. do
  145. cb_drag := act
  146. if cb_drag /= Void then
  147. operation := 1
  148. else
  149. operation := 0
  150. end
  151. iup_open.set_callback (Current, "DRAG_CB", "NONEEDED", operation)
  152. end
  153. set_cb_value_changed (act: detachable FUNCTION[TUPLE[IUP_COLOR_BROWSER], STRING])
  154. -- Called after the value was interactively changed by the user. It is
  155. -- called whenever a CHANGE_CB or a DRAG_CB would also be called, it is
  156. -- just called after them.
  157. --
  158. -- ih: identifier of the element that activated the event.
  159. local
  160. operation: INTEGER
  161. do
  162. cb_valuechanged := act
  163. if cb_valuechanged /= Void then
  164. operation := 1
  165. else
  166. operation := 0
  167. end
  168. iup_open.set_callback (Current, "VALUECHANGED_CB", "NONEEDED", operation)
  169. end
  170. feature {IUP}
  171. execute_change (red, green, blue: INTEGER): STRING
  172. do
  173. if attached cb_change as int_cb then
  174. Result := int_cb.item([Current, red, green, blue])
  175. else
  176. Result := "IUP_DEFAULT"
  177. end
  178. end
  179. execute_drag (red, green, blue: INTEGER): STRING
  180. do
  181. if attached cb_drag as int_cb then
  182. Result := int_cb.item([Current, red, green, blue])
  183. else
  184. Result := "IUP_DEFAULT"
  185. end
  186. end
  187. execute_valuechanged: STRING
  188. do
  189. if attached cb_valuechanged as int_cb then
  190. Result := int_cb.item([Current])
  191. else
  192. Result := "IUP_DEFAULT"
  193. end
  194. end
  195. feature {NONE}
  196. -- Callbakcs
  197. cb_change: detachable FUNCTION[TUPLE[IUP_COLOR_BROWSER, INTEGER, INTEGER, INTEGER], STRING]
  198. cb_drag: detachable FUNCTION[TUPLE[IUP_COLOR_BROWSER, INTEGER, INTEGER, INTEGER], STRING]
  199. cb_valuechanged: detachable FUNCTION[TUPLE[IUP_COLOR_BROWSER], STRING]
  200. -- Internals
  201. int_color_browser: POINTER
  202. external
  203. "C inline use %"eiffel-iup.h%""
  204. alias
  205. "return IupColorBrowser();"
  206. end
  207. end -- class IUP_COLOR_BROWSER
  208. -- The MIT License (MIT)
  209. -- Copyright (c) 2019, 2020 by German A. Arias
  210. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  211. -- of this software and associated documentation files (the "Software"), to deal
  212. -- in the Software without restriction, including without limitation the rights
  213. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  214. -- copies of the Software, and to permit persons to whom the Software is
  215. -- furnished to do so, subject to the following conditions:
  216. --
  217. -- The above copyright notice and this permission notice shall be included in
  218. -- all copies or substantial portions of the Software.
  219. --
  220. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  221. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  222. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  223. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  224. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  225. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  226. -- SOFTWARE.