color.go 6.0 KB

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