color.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Color extension
  2. package al
  3. /*
  4. #cgo pkg-config: allegro_color-5
  5. #cgo CFLAGS: -I/usr/local/include
  6. #cgo linux LDFLAGS: -lc_nonshared
  7. #include <stdlib.h>
  8. #include <allegro5/allegro.h>
  9. #include <allegro5/allegro_color.h>
  10. #include "helpers.h"
  11. */
  12. import "C"
  13. // import "runtime"
  14. // import "unsafe"
  15. // Gets the version of the color addon
  16. func ColorVersion() uint32 {
  17. return uint32(C.al_get_allegro_color_version())
  18. }
  19. // Converts HSV color components to RGB
  20. func HsvToRgb(h, s, v float32) (r, g, b float32) {
  21. var cr, cg, cb C.float
  22. C.al_color_hsv_to_rgb(C.float(h), C.float(s), C.float(v), &cr, &cg, &cb)
  23. return float32(cr), float32(cg), float32(cb)
  24. }
  25. // Converts RGB color components to HSL
  26. func RgbToHsl(r, g, b float32) (h, s, l float32) {
  27. var ch, cs, cl C.float
  28. C.al_color_rgb_to_hsl(C.float(r), C.float(g), C.float(b), &ch, &cs, &cl)
  29. return float32(ch), float32(cs), float32(cl)
  30. }
  31. // Converts RGB color components to HSV
  32. func RgbToHsv(r, g, b float32) (h, s, v float32) {
  33. var ch, cs, cv C.float
  34. C.al_color_rgb_to_hsl(C.float(r), C.float(g), C.float(b), &ch, &cs, &cv)
  35. return float32(ch), float32(cs), float32(cv)
  36. }
  37. // Converts HSL color components to RGB
  38. func HslToRgb(h, s, l float32) (r, g, b float32) {
  39. var cr, cg, cb C.float
  40. C.al_color_hsl_to_rgb(C.float(h), C.float(s), C.float(l), &cr, &cg, &cb)
  41. return float32(cr), float32(cg), float32(cb)
  42. }
  43. // Converts a X11 color name to RGB
  44. func NameToRgb(name string) (r, g, b float32) {
  45. var cr, cg, cb C.float
  46. cname := cstr(name)
  47. defer cstrFree(cname)
  48. C.al_color_name_to_rgb(cname, &cr, &cg, &cb)
  49. return float32(cr), float32(cg), float32(cb)
  50. }
  51. // Converts RGB color components to an X11 color name
  52. func RgbToName(r, g, b float32) (name string) {
  53. return gostr(C.al_color_rgb_to_name(C.float(r), C.float(g), C.float(b)))
  54. }
  55. // Converts RGB color components to CMYK
  56. func RgbToCmyk(r, g, b float32) (c, m, y, k float32) {
  57. var cc, cm, cy, ck C.float
  58. C.al_color_rgb_to_cmyk(C.float(r), C.float(g), C.float(b), &cc, &cm, &cy, &ck)
  59. return float32(cc), float32(cm), float32(cy), float32(ck)
  60. }
  61. // Converts CMYK color components to RGB
  62. func CmykToRgb(c, m, y, k float32) (r, g, b float32) {
  63. var cr, cg, cb C.float
  64. C.al_color_cmyk_to_rgb(C.float(c), C.float(m), C.float(y), C.float(k), &cr, &cg, &cb)
  65. return float32(cr), float32(cg), float32(cb)
  66. }
  67. // Converts RGB color components to YUV
  68. func RgbToYuv(r, g, b float32) (y, u, v float32) {
  69. var cy, cu, cv C.float
  70. C.al_color_rgb_to_yuv(C.float(r), C.float(g), C.float(b), &cy, &cu, &cv)
  71. return float32(cy), float32(cu), float32(cv)
  72. }
  73. // Converts HSL color components to RGB
  74. func YuvToRgb(y, u, v float32) (r, g, b float32) {
  75. var cr, cg, cb C.float
  76. C.al_color_yuv_to_rgb(C.float(y), C.float(u), C.float(v), &cr, &cg, &cb)
  77. return float32(cr), float32(cg), float32(cb)
  78. }
  79. // Converts RGB color components to HTML notation
  80. func RgbToHtml(r, g, b float32) (html string) {
  81. chtml := alMalloc(8)
  82. defer alFree(chtml)
  83. C.al_color_rgb_to_html(C.float(r), C.float(g), C.float(b), (*C.char)(chtml))
  84. return gostr((*C.char)(chtml))
  85. }
  86. // Converts HTML notation to RGB color components
  87. // Converts a X11 color name to RGB
  88. func HtmlToRgb(html string) (r, g, b float32) {
  89. var cr, cg, cb C.float
  90. chtml := cstr(html)
  91. defer cstrFree(chtml)
  92. C.al_color_name_to_rgb(chtml, &cr, &cg, &cb)
  93. return float32(cr), float32(cg), float32(cb)
  94. }
  95. // Creates a color from YUV components
  96. func ColorYuv(y, u, v float32) Color {
  97. return wrapColor(C.al_color_yuv(C.float(y), C.float(u), C.float(v)))
  98. }
  99. // Creates a color from Cmyk components
  100. func ColorCmyk(c, m, y, k float32) Color {
  101. return wrapColor(C.al_color_cmyk(C.float(c), C.float(m), C.float(y), C.float(k)))
  102. }
  103. // Creates a color from HSL components
  104. func ColorHsl(h, s, l float32) Color {
  105. return wrapColor(C.al_color_hsl(C.float(h), C.float(s), C.float(l)))
  106. }
  107. // Creates a color from HSV components
  108. func ColorHsv(h, s, v float32) Color {
  109. return wrapColor(C.al_color_hsv(C.float(h), C.float(s), C.float(v)))
  110. }
  111. // Creates a color from an X11 color name
  112. func ColorName(s string) Color {
  113. cs := cstr(s)
  114. defer cstrFree(cs)
  115. return wrapColor(C.al_color_name(cs))
  116. }
  117. // Creates a color from HTML notation
  118. func ColorHtml(s string) Color {
  119. cs := cstr(s)
  120. defer cstrFree(cs)
  121. return wrapColor(C.al_color_html(cs))
  122. }