color.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // color and color pixel format support
  2. package al
  3. /*
  4. #include <stdlib.h>
  5. #include <allegro5/allegro.h>
  6. #include "helpers.h"
  7. #include "callbacks.h"
  8. */
  9. import "C"
  10. // Color type
  11. type Color C.ALLEGRO_COLOR
  12. // Convert from
  13. func wrapColor(color C.ALLEGRO_COLOR) Color {
  14. return Color(color)
  15. }
  16. // Convert to C
  17. func (self Color) toC() C.ALLEGRO_COLOR {
  18. return C.ALLEGRO_COLOR(self)
  19. }
  20. // Creates a new color
  21. func CreateColor(r, g, b, a float32) Color {
  22. return Color{C.float(r), C.float(g), C.float(b), C.float(a)}
  23. }
  24. // Returns the R component of the color self.
  25. func (self Color) R() float32 {
  26. return float32(self.r)
  27. }
  28. // Returns the G component of the color self.
  29. func (self Color) G() float32 {
  30. return float32(self.g)
  31. }
  32. // Returns the B component of the color self.
  33. func (self Color) B() float32 {
  34. return float32(self.b)
  35. }
  36. // Returns the A component of the color self.
  37. func (self Color) A() float32 {
  38. return float32(self.a)
  39. }
  40. type PixelFormat C.enum_ALLEGRO_PIXEL_FORMAT
  41. const(
  42. PIXEL_FORMAT_ANY = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY )
  43. PIXEL_FORMAT_ANY_NO_ALPHA = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_NO_ALPHA )
  44. PIXEL_FORMAT_ANY_WITH_ALPHA = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA )
  45. PIXEL_FORMAT_ANY_15_NO_ALPHA = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_15_NO_ALPHA )
  46. PIXEL_FORMAT_ANY_16_NO_ALPHA = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_16_NO_ALPHA )
  47. PIXEL_FORMAT_ANY_16_WITH_ALPHA = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_16_WITH_ALPHA )
  48. PIXEL_FORMAT_ANY_24_NO_ALPHA = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_24_NO_ALPHA )
  49. PIXEL_FORMAT_ANY_32_NO_ALPHA = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_32_NO_ALPHA )
  50. PIXEL_FORMAT_ANY_32_WITH_ALPHA = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_32_WITH_ALPHA )
  51. PIXEL_FORMAT_ARGB_8888 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ARGB_8888 )
  52. PIXEL_FORMAT_RGBA_8888 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGBA_8888 )
  53. PIXEL_FORMAT_ARGB_4444 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ARGB_4444 )
  54. PIXEL_FORMAT_RGB_888 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGB_888 )
  55. PIXEL_FORMAT_RGB_565 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGB_565 )
  56. PIXEL_FORMAT_RGB_555 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGB_555 )
  57. PIXEL_FORMAT_RGBA_5551 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGBA_5551 )
  58. PIXEL_FORMAT_ARGB_1555 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ARGB_1555 )
  59. PIXEL_FORMAT_ABGR_8888 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ABGR_8888 )
  60. PIXEL_FORMAT_XBGR_8888 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_XBGR_8888 )
  61. PIXEL_FORMAT_BGR_888 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_BGR_888 )
  62. PIXEL_FORMAT_BGR_565 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_BGR_565 )
  63. PIXEL_FORMAT_BGR_555 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_BGR_555 )
  64. PIXEL_FORMAT_RGBX_8888 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGBX_8888 )
  65. PIXEL_FORMAT_XRGB_8888 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_XRGB_8888 )
  66. PIXEL_FORMAT_ABGR_F32 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ABGR_F32 )
  67. PIXEL_FORMAT_ABGR_8888_LE = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE )
  68. PIXEL_FORMAT_RGBA_4444 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGBA_4444 )
  69. PIXEL_FORMAT_SINGLE_CHANNEL_8 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_SINGLE_CHANNEL_8 )
  70. PIXEL_FORMAT_COMPRESSED_RGBA_DXT1= PixelFormat(C.ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT1)
  71. PIXEL_FORMAT_COMPRESSED_RGBA_DXT3= PixelFormat(C.ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT3)
  72. PIXEL_FORMAT_COMPRESSED_RGBA_DXT5= PixelFormat(C.ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT5)
  73. NUM_PIXEL_FORMATS = PixelFormat(C.ALLEGRO_NUM_PIXEL_FORMATS )
  74. )
  75. func MapRGB(r, g, b uint8) Color {
  76. return wrapColor(C.al_map_rgb(C.uchar(r), C.uchar(g), C.uchar(b)))
  77. }
  78. func MapRGBA(r, g, b, a uint8) Color {
  79. return wrapColor(C.al_map_rgba(C.uchar(r), C.uchar(g), C.uchar(b), C.uchar(a)))
  80. }
  81. func PremulRGBA(r, g, b, a uint8) Color {
  82. return wrapColor(C.al_premul_rgba(C.uchar(r), C.uchar(g), C.uchar(b), C.uchar(a)))
  83. }
  84. func MapRGBF(r, g, b float32) Color {
  85. return wrapColor(C.al_map_rgb_f(C.float(r), C.float(g), C.float(b)))
  86. }
  87. func MapRGBAF(r, g, b, a float32) Color {
  88. return wrapColor(C.al_map_rgba_f(C.float(r), C.float(g), C.float(b), C.float(a)))
  89. }
  90. func PremulRGBAF(r, g, b, a float32) Color {
  91. return wrapColor(C.al_premul_rgba_f(C.float(r), C.float(g), C.float(b), C.float(a)))
  92. }
  93. func (color Color) UnmapRGB() (r, g, b uint8) {
  94. var cr, cg, cb C.uchar
  95. C.al_unmap_rgb(color.toC(), &cr, &cg, &cb)
  96. r = uint8(cr)
  97. g = uint8(cg)
  98. b = uint8(cb)
  99. return r, g, b
  100. }
  101. func (color Color) UnmapRGBA() (r, g, b, a uint8) {
  102. var cr, cg, cb, ca C.uchar
  103. C.al_unmap_rgba(color.toC(), &cr, &cg, &cb, &ca)
  104. r = uint8(cr)
  105. g = uint8(cg)
  106. b = uint8(cb)
  107. a = uint8(ca)
  108. return r, g, b, a
  109. }
  110. func (color Color) UnmapRGBF() (r, g, b float32) {
  111. var cr, cg, cb C.float
  112. C.al_unmap_rgb_f(color.toC(), &cr, &cg, &cb)
  113. r = float32(cr)
  114. g = float32(cg)
  115. b = float32(cb)
  116. return r, g, b
  117. }
  118. func (color Color) UnmapRGBAF() (r, g, b, a float32) {
  119. var cr, cg, cb, ca C.float
  120. C.al_unmap_rgba_f(color.toC(), &cr, &cg, &cb, &ca)
  121. r = float32(cr)
  122. g = float32(cg)
  123. b = float32(cb)
  124. a = float32(ca)
  125. return r, g, b, a
  126. }
  127. func (format PixelFormat) PixelSize() int {
  128. return int(C.al_get_pixel_size(C.int(format)))
  129. }
  130. func (format PixelFormat) FormatBits() int {
  131. return int(C.al_get_pixel_format_bits(C.int(format)))
  132. }
  133. func (format PixelFormat) BlockSize() int {
  134. return int(C.al_get_pixel_block_size(C.int(format)))
  135. }
  136. func (format PixelFormat) BlockWidth() int {
  137. return int(C.al_get_pixel_block_width(C.int(format)))
  138. }
  139. func (format PixelFormat) BlockHeight() int {
  140. return int(C.al_get_pixel_block_height(C.int(format)))
  141. }