Browse Source

Improve color and pixel format wrappers

Beoran 6 years ago
parent
commit
bc0d6e1d6f
3 changed files with 268 additions and 135 deletions
  1. 143 0
      al/allegro_color.go
  2. 125 98
      al/color.go
  3. 0 37
      al/display.go

+ 143 - 0
al/allegro_color.go

@@ -0,0 +1,143 @@
+// Color extension
+package al
+
+/*
+#cgo pkg-config: allegro_color-5
+#cgo CFLAGS: -I/usr/local/include
+#cgo linux LDFLAGS: -lc_nonshared
+#include <stdlib.h>
+#include <allegro5/allegro.h>
+#include <allegro5/allegro_color.h>
+#include "helpers.h"
+*/
+import "C"
+
+// import "runtime"
+// import "unsafe"
+
+// Gets the version of the color addon
+func ColorVersion() uint32 {
+	return uint32(C.al_get_allegro_color_version())
+}
+
+// Converts HSV color components to RGB 
+func HsvToRgb(h, s, v float32) (r, g, b float32) {
+	var cr, cg, cb C.float
+	C.al_color_hsv_to_rgb(C.float(h), C.float(s), C.float(v), &cr, &cg, &cb)
+	return float32(cr), float32(cg), float32(cb)
+}
+
+// Converts RGB color components to HSL
+func RgbToHsl(r, g, b float32) (h, s, l float32) {
+	var ch, cs, cl C.float
+	C.al_color_rgb_to_hsl(C.float(r), C.float(g), C.float(b), &ch, &cs, &cl)
+	return float32(ch), float32(cs), float32(cl)
+}
+
+// Converts RGB color components to HSV
+func RgbToHsv(r, g, b float32) (h, s, v float32) {
+	var ch, cs, cv C.float
+	C.al_color_rgb_to_hsl(C.float(r), C.float(g), C.float(b), &ch, &cs, &cv)
+	return float32(ch), float32(cs), float32(cv)
+}
+
+// Converts HSL color components to RGB 
+func HslToRgb(h, s, l float32) (r, g, b float32) {
+	var cr, cg, cb C.float
+	C.al_color_hsl_to_rgb(C.float(h), C.float(s), C.float(l), &cr, &cg, &cb)
+	return float32(cr), float32(cg), float32(cb)
+}
+
+// Converts a X11 color name to RGB 
+func NameToRgb(name string) (r, g, b float32) {
+	var cr, cg, cb C.float
+	cname := cstr(name)
+	defer cstrFree(cname)
+	C.al_color_name_to_rgb(cname, &cr, &cg, &cb)
+	return float32(cr), float32(cg), float32(cb)
+}
+
+// Converts RGB color components to an X11 color name 
+func RgbToName(r, g, b float32) (name string) {
+	return gostr(C.al_color_rgb_to_name(C.float(r), C.float(g), C.float(b)))
+}
+
+// Converts RGB color components to CMYK
+func RgbToCmyk(r, g, b float32) (c, m, y, k float32) {
+	var cc, cm, cy, ck C.float
+	C.al_color_rgb_to_cmyk(C.float(r), C.float(g), C.float(b), &cc, &cm, &cy, &ck)
+	return float32(cc), float32(cm), float32(cy), float32(ck)
+}
+
+// Converts CMYK color components to RGB
+func CmykToRgb(c, m, y, k float32) (r, g, b float32) {
+	var cr, cg, cb C.float
+	C.al_color_cmyk_to_rgb(C.float(c), C.float(m), C.float(y), C.float(k), &cr, &cg, &cb)
+	return float32(cr), float32(cg), float32(cb)
+}
+
+// Converts RGB color components to YUV
+func RgbToYuv(r, g, b float32) (y, u, v float32) {
+	var cy, cu, cv C.float
+	C.al_color_rgb_to_yuv(C.float(r), C.float(g), C.float(b), &cy, &cu, &cv)
+	return float32(cy), float32(cu), float32(cv)
+}
+
+// Converts HSL color components to RGB 
+func YuvToRgb(y, u, v float32) (r, g, b float32) {
+	var cr, cg, cb C.float
+	C.al_color_yuv_to_rgb(C.float(y), C.float(u), C.float(v), &cr, &cg, &cb)
+	return float32(cr), float32(cg), float32(cb)
+}
+
+// Converts RGB color components to HTML notation
+func RgbToHtml(r, g, b float32) (html string) {
+	chtml := alMalloc(8)
+	defer alFree(chtml)
+	C.al_color_rgb_to_html(C.float(r), C.float(g), C.float(b), (*C.char)(chtml))
+	return gostr((*C.char)(chtml))
+}
+
+// Converts HTML notation to RGB color components
+// Converts a X11 color name to RGB 
+func HtmlToRgb(html string) (r, g, b float32) {
+	var cr, cg, cb C.float
+	chtml := cstr(html)
+	defer cstrFree(chtml)
+	C.al_color_name_to_rgb(chtml, &cr, &cg, &cb)
+	return float32(cr), float32(cg), float32(cb)
+}
+
+// Creates a color from YUV components
+func ColorYuv(y, u, v float32) Color {
+	return wrapColor(C.al_color_yuv(C.float(y), C.float(u), C.float(v)))
+}
+
+// Creates a color from Cmyk components
+func ColorCmyk(c, m, y, k float32) Color {
+	return wrapColor(C.al_color_cmyk(C.float(c), C.float(m), C.float(y), C.float(k)))
+}
+
+// Creates a color from HSL components
+func ColorHsl(h, s, l float32) Color {
+	return wrapColor(C.al_color_hsl(C.float(h), C.float(s), C.float(l)))
+}
+
+// Creates a color from HSV components
+func ColorHsv(h, s, v float32) Color {
+	return wrapColor(C.al_color_hsv(C.float(h), C.float(s), C.float(v)))
+}
+
+// Creates a color from an X11 color name 
+func ColorName(s string) Color {
+	cs := cstr(s)
+	defer cstrFree(cs)
+	return wrapColor(C.al_color_name(cs))
+}
+
+// Creates a color from HTML notation
+func ColorHtml(s string) Color {
+	cs := cstr(s)
+	defer cstrFree(cs)
+	return wrapColor(C.al_color_html(cs))
+}

+ 125 - 98
al/color.go

@@ -1,143 +1,170 @@
-// Color extension
+// color and color pixel format support
 package al
 
 /*
-#cgo pkg-config: allegro_color-5
-#cgo CFLAGS: -I/usr/local/include
-#cgo linux LDFLAGS: -lc_nonshared
 #include <stdlib.h>
 #include <allegro5/allegro.h>
-#include <allegro5/allegro_color.h>
 #include "helpers.h"
+#include "callbacks.h"
 */
 import "C"
 
-// import "runtime"
-// import "unsafe"
+// Color type
+type Color C.ALLEGRO_COLOR
 
-// Gets the version of the color addon
-func ColorVersion() uint32 {
-	return uint32(C.al_get_allegro_color_version())
+// Convert from
+func wrapColor(color C.ALLEGRO_COLOR) Color {
+    return Color(color)
 }
 
-// Converts HSV color components to RGB 
-func HsvToRgb(h, s, v float32) (r, g, b float32) {
-	var cr, cg, cb C.float
-	C.al_color_hsv_to_rgb(C.float(h), C.float(s), C.float(v), &cr, &cg, &cb)
-	return float32(cr), float32(cg), float32(cb)
+// Convert to C
+func (self Color) toC() C.ALLEGRO_COLOR {
+    return C.ALLEGRO_COLOR(self)
 }
 
-// Converts RGB color components to HSL
-func RgbToHsl(r, g, b float32) (h, s, l float32) {
-	var ch, cs, cl C.float
-	C.al_color_rgb_to_hsl(C.float(r), C.float(g), C.float(b), &ch, &cs, &cl)
-	return float32(ch), float32(cs), float32(cl)
+// Creates a new color 
+func CreateColor(r, g, b, a float32) Color {
+    return Color{C.float(r), C.float(g), C.float(b), C.float(a)}
 }
 
-// Converts RGB color components to HSV
-func RgbToHsv(r, g, b float32) (h, s, v float32) {
-	var ch, cs, cv C.float
-	C.al_color_rgb_to_hsl(C.float(r), C.float(g), C.float(b), &ch, &cs, &cv)
-	return float32(ch), float32(cs), float32(cv)
+// Returns the R component of the color self.
+func (self Color) R() float32 {
+    return float32(self.r)
 }
 
-// Converts HSL color components to RGB 
-func HslToRgb(h, s, l float32) (r, g, b float32) {
-	var cr, cg, cb C.float
-	C.al_color_hsl_to_rgb(C.float(h), C.float(s), C.float(l), &cr, &cg, &cb)
-	return float32(cr), float32(cg), float32(cb)
+// Returns the G component of the color self.
+func (self Color) G() float32 {
+    return float32(self.g)
 }
 
-// Converts a X11 color name to RGB 
-func NameToRgb(name string) (r, g, b float32) {
-	var cr, cg, cb C.float
-	cname := cstr(name)
-	defer cstrFree(cname)
-	C.al_color_name_to_rgb(cname, &cr, &cg, &cb)
-	return float32(cr), float32(cg), float32(cb)
+// Returns the B component of the color self.
+func (self Color) B() float32 {
+    return float32(self.b)
 }
 
-// Converts RGB color components to an X11 color name 
-func RgbToName(r, g, b float32) (name string) {
-	return gostr(C.al_color_rgb_to_name(C.float(r), C.float(g), C.float(b)))
+// Returns the A component of the color self.
+func (self Color) A() float32 {
+    return float32(self.a)
 }
 
-// Converts RGB color components to CMYK
-func RgbToCmyk(r, g, b float32) (c, m, y, k float32) {
-	var cc, cm, cy, ck C.float
-	C.al_color_rgb_to_cmyk(C.float(r), C.float(g), C.float(b), &cc, &cm, &cy, &ck)
-	return float32(cc), float32(cm), float32(cy), float32(ck)
-}
+type PixelFormat C.enum_ALLEGRO_PIXEL_FORMAT
 
-// Converts CMYK color components to RGB
-func CmykToRgb(c, m, y, k float32) (r, g, b float32) {
-	var cr, cg, cb C.float
-	C.al_color_cmyk_to_rgb(C.float(c), C.float(m), C.float(y), C.float(k), &cr, &cg, &cb)
-	return float32(cr), float32(cg), float32(cb)
-}
+const(
+   PIXEL_FORMAT_ANY                 = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY                 )
+   PIXEL_FORMAT_ANY_NO_ALPHA        = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_NO_ALPHA        )
+   PIXEL_FORMAT_ANY_WITH_ALPHA      = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA      )
+   PIXEL_FORMAT_ANY_15_NO_ALPHA     = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_15_NO_ALPHA     )
+   PIXEL_FORMAT_ANY_16_NO_ALPHA     = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_16_NO_ALPHA     )
+   PIXEL_FORMAT_ANY_16_WITH_ALPHA   = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_16_WITH_ALPHA   )
+   PIXEL_FORMAT_ANY_24_NO_ALPHA     = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_24_NO_ALPHA     )
+   PIXEL_FORMAT_ANY_32_NO_ALPHA     = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_32_NO_ALPHA     )
+   PIXEL_FORMAT_ANY_32_WITH_ALPHA   = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ANY_32_WITH_ALPHA   )
+   PIXEL_FORMAT_ARGB_8888           = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ARGB_8888           )
+   PIXEL_FORMAT_RGBA_8888           = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGBA_8888           )
+   PIXEL_FORMAT_ARGB_4444           = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ARGB_4444           )
+   PIXEL_FORMAT_RGB_888             = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGB_888             )
+   PIXEL_FORMAT_RGB_565             = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGB_565             )
+   PIXEL_FORMAT_RGB_555             = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGB_555             )
+   PIXEL_FORMAT_RGBA_5551           = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGBA_5551           )
+   PIXEL_FORMAT_ARGB_1555           = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ARGB_1555           )
+   PIXEL_FORMAT_ABGR_8888           = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ABGR_8888           )
+   PIXEL_FORMAT_XBGR_8888           = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_XBGR_8888           )
+   PIXEL_FORMAT_BGR_888             = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_BGR_888             )
+   PIXEL_FORMAT_BGR_565             = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_BGR_565             )
+   PIXEL_FORMAT_BGR_555             = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_BGR_555             )
+   PIXEL_FORMAT_RGBX_8888           = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGBX_8888           )
+   PIXEL_FORMAT_XRGB_8888           = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_XRGB_8888           )
+   PIXEL_FORMAT_ABGR_F32            = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ABGR_F32            )
+   PIXEL_FORMAT_ABGR_8888_LE        = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE        )
+   PIXEL_FORMAT_RGBA_4444           = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_RGBA_4444           )
+   PIXEL_FORMAT_SINGLE_CHANNEL_8    = PixelFormat(C.ALLEGRO_PIXEL_FORMAT_SINGLE_CHANNEL_8    )
+   PIXEL_FORMAT_COMPRESSED_RGBA_DXT1= PixelFormat(C.ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT1)
+   PIXEL_FORMAT_COMPRESSED_RGBA_DXT3= PixelFormat(C.ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT3)
+   PIXEL_FORMAT_COMPRESSED_RGBA_DXT5= PixelFormat(C.ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT5)
+   NUM_PIXEL_FORMATS                = PixelFormat(C.ALLEGRO_NUM_PIXEL_FORMATS                )
+)
 
-// Converts RGB color components to YUV
-func RgbToYuv(r, g, b float32) (y, u, v float32) {
-	var cy, cu, cv C.float
-	C.al_color_rgb_to_yuv(C.float(r), C.float(g), C.float(b), &cy, &cu, &cv)
-	return float32(cy), float32(cu), float32(cv)
+
+func MapRGB(r, g, b uint8) Color {
+    return wrapColor(C.al_map_rgb(C.uchar(r), C.uchar(g), C.uchar(b)))
 }
 
-// Converts HSL color components to RGB 
-func YuvToRgb(y, u, v float32) (r, g, b float32) {
-	var cr, cg, cb C.float
-	C.al_color_yuv_to_rgb(C.float(y), C.float(u), C.float(v), &cr, &cg, &cb)
-	return float32(cr), float32(cg), float32(cb)
+func MapRGBA(r, g, b, a uint8) Color {
+    return wrapColor(C.al_map_rgba(C.uchar(r), C.uchar(g), C.uchar(b), C.uchar(a)))
 }
 
-// Converts RGB color components to HTML notation
-func RgbToHtml(r, g, b float32) (html string) {
-	chtml := alMalloc(8)
-	defer alFree(chtml)
-	C.al_color_rgb_to_html(C.float(r), C.float(g), C.float(b), (*C.char)(chtml))
-	return gostr((*C.char)(chtml))
+func PremulRGBA(r, g, b, a uint8) Color {
+    return wrapColor(C.al_premul_rgba(C.uchar(r), C.uchar(g), C.uchar(b), C.uchar(a)))
 }
 
-// Converts HTML notation to RGB color components
-// Converts a X11 color name to RGB 
-func HtmlToRgb(html string) (r, g, b float32) {
-	var cr, cg, cb C.float
-	chtml := cstr(html)
-	defer cstrFree(chtml)
-	C.al_color_name_to_rgb(chtml, &cr, &cg, &cb)
-	return float32(cr), float32(cg), float32(cb)
+
+func MapRGBF(r, g, b float32) Color {
+    return wrapColor(C.al_map_rgb_f(C.float(r), C.float(g), C.float(b)))
 }
 
-// Creates a color from YUV components
-func ColorYuv(y, u, v float32) Color {
-	return wrapColor(C.al_color_yuv(C.float(y), C.float(u), C.float(v)))
+func MapRGBAF(r, g, b, a float32) Color {
+    return wrapColor(C.al_map_rgba_f(C.float(r), C.float(g), C.float(b), C.float(a)))
 }
 
-// Creates a color from Cmyk components
-func ColorCmyk(c, m, y, k float32) Color {
-	return wrapColor(C.al_color_cmyk(C.float(c), C.float(m), C.float(y), C.float(k)))
+func PremulRGBAF(r, g, b, a float32) Color {
+    return wrapColor(C.al_premul_rgba_f(C.float(r), C.float(g), C.float(b), C.float(a)))
 }
 
-// Creates a color from HSL components
-func ColorHsl(h, s, l float32) Color {
-	return wrapColor(C.al_color_hsl(C.float(h), C.float(s), C.float(l)))
+func (color Color) UnmapRGB() (r, g, b uint8) {
+    var cr, cg, cb C.uchar
+    C.al_unmap_rgb(color.toC(), &cr, &cg, &cb)
+    r = uint8(cr)
+    g = uint8(cg)
+    b = uint8(cb)
+    return r, g, b
 }
 
-// Creates a color from HSV components
-func ColorHsv(h, s, v float32) Color {
-	return wrapColor(C.al_color_hsv(C.float(h), C.float(s), C.float(v)))
+func (color Color) UnmapRGBA() (r, g, b, a uint8) {
+    var cr, cg, cb, ca C.uchar
+    C.al_unmap_rgba(color.toC(), &cr, &cg, &cb, &ca)
+    r = uint8(cr)
+    g = uint8(cg)
+    b = uint8(cb)
+    a = uint8(ca)
+    return r, g, b, a
 }
 
-// Creates a color from an X11 color name 
-func ColorName(s string) Color {
-	cs := cstr(s)
-	defer cstrFree(cs)
-	return wrapColor(C.al_color_name(cs))
+func (color Color) UnmapRGBF() (r, g, b float32) {
+    var cr, cg, cb C.float
+    C.al_unmap_rgb_f(color.toC(), &cr, &cg, &cb)
+    r = float32(cr)
+    g = float32(cg)
+    b = float32(cb)
+    return r, g, b
 }
 
-// Creates a color from HTML notation
-func ColorHtml(s string) Color {
-	cs := cstr(s)
-	defer cstrFree(cs)
-	return wrapColor(C.al_color_html(cs))
+func (color Color) UnmapRGBAF() (r, g, b, a float32) {
+    var cr, cg, cb, ca C.float
+    C.al_unmap_rgba_f(color.toC(), &cr, &cg, &cb, &ca)
+    r = float32(cr)
+    g = float32(cg)
+    b = float32(cb)
+    a = float32(ca)
+    return r, g, b, a
 }
+
+func (format PixelFormat) PixelSize() int {
+    return int(C.al_get_pixel_size(C.int(format)))
+} 
+
+func (format PixelFormat) FormatBits() int {
+    return int(C.al_get_pixel_format_bits(C.int(format)))
+} 
+
+func (format PixelFormat) BlockSize() int {
+    return int(C.al_get_pixel_block_size(C.int(format)))
+} 
+
+func (format PixelFormat) BlockWidth() int {
+    return int(C.al_get_pixel_block_width(C.int(format)))
+} 
+
+func (format PixelFormat) BlockHeight() int {
+    return int(C.al_get_pixel_block_height(C.int(format)))
+} 
+

+ 0 - 37
al/display.go

@@ -199,43 +199,6 @@ func (self *Display) Flip() {
     C.al_flip_display()
 }
 
-// Color type
-type Color C.ALLEGRO_COLOR
-
-// Convert from
-func wrapColor(color C.ALLEGRO_COLOR) Color {
-    return Color(color)
-}
-
-// Convert to C
-func (self Color) toC() C.ALLEGRO_COLOR {
-    return C.ALLEGRO_COLOR(self)
-}
-
-// Creates a new color 
-func CreateColor(r, g, b, a float32) Color {
-    return Color{C.float(r), C.float(g), C.float(b), C.float(a)}
-}
-
-// Returns the R component of the color self.
-func (self Color) R() float32 {
-    return float32(self.r)
-}
-
-// Returns the G component of the color self.
-func (self Color) G() float32 {
-    return float32(self.g)
-}
-
-// Returns the B component of the color self.
-func (self Color) B() float32 {
-    return float32(self.b)
-}
-
-// Returns the A component of the color self.
-func (self Color) A() float32 {
-    return float32(self.a)
-}
 
 // Fills the current active display with a color
 func ClearToColor(color Color) {