-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
469 additions
and
425 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package bitmap | ||
|
||
import ( | ||
"fmt" | ||
"image/color" | ||
|
||
// Modules | ||
gopi "github.com/djthorpe/gopi/v3" | ||
) | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// INTERFACE | ||
|
||
type ColorModel interface { | ||
color.Model | ||
|
||
Format() gopi.SurfaceFormat | ||
BitsPerPixel() uint8 | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// TYPES | ||
|
||
type RGBA32 uint32 | ||
|
||
type model struct { | ||
fmt gopi.SurfaceFormat | ||
bpp uint8 | ||
fn func(color.Color) color.Color | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// INIT | ||
|
||
func init() { | ||
// Register Color Models | ||
RegisterColorModel(gopi.SURFACE_FMT_RGBA32, NewModel(gopi.SURFACE_FMT_RGBA32, 32, toRGBA32)) | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// LIFECYCLE | ||
|
||
func NewModel(fmt gopi.SurfaceFormat, bpp uint8, fn func(color.Color) color.Color) ColorModel { | ||
m := new(model) | ||
m.fmt = fmt | ||
m.fn = fn | ||
m.bpp = bpp | ||
return m | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// METHODS | ||
|
||
func (this *model) Convert(c color.Color) color.Color { | ||
return this.fn(c) | ||
} | ||
|
||
func (this *model) Format() gopi.SurfaceFormat { | ||
return this.fmt | ||
} | ||
|
||
func (this *model) BitsPerPixel() uint8 { | ||
return this.bpp | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// STRINGIFY | ||
|
||
func (this *model) String() string { | ||
str := "<bitmap.colormodel" | ||
str += fmt.Sprint(" fmt=", this.Format()) | ||
str += fmt.Sprint(" bits_per_pixel=", this.BitsPerPixel()) | ||
return str + ">" | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// RGBA32 | ||
|
||
func toRGBA32(c color.Color) color.Color { | ||
if c, ok := c.(RGBA32); ok { | ||
return c | ||
} | ||
r, g, b, a := c.RGBA() | ||
return RGBA32(r<<16&0xFF000000) | RGBA32(g<<8&0x00FF0000) | RGBA32(b<<0&0x0000FF00) | RGBA32(a>>8&0x000000FF) | ||
} | ||
|
||
func (p RGBA32) RGBA() (uint32, uint32, uint32, uint32) { | ||
r := uint32(byte(p>>24)) * 0x0101 | ||
g := uint32(byte(p>>16)) * 0x0101 | ||
b := uint32(byte(p>>8)) * 0x0101 | ||
a := uint32(byte(p)) * 0x0101 | ||
return r, g, b, a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package bitmap_test | ||
|
||
import ( | ||
"image/color" | ||
"testing" | ||
|
||
// Frameworks | ||
gopi "github.com/djthorpe/gopi/v3" | ||
bitmap "github.com/djthorpe/gopi/v3/pkg/graphics/bitmap" | ||
) | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// TESTS | ||
|
||
func Test_Color_001(t *testing.T) { | ||
tests := []struct { | ||
c color.Color | ||
r, g, b, a uint32 | ||
}{ | ||
{color.Black, 0x0000, 0x0000, 0x0000, 0xFFFF}, | ||
{color.White, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF}, | ||
{color.RGBA{0xFF, 0x00, 0x00, 0xFF}, 0xFFFF, 0x0000, 0x0000, 0xFFFF}, // Red | ||
{color.RGBA{0x00, 0xFF, 0x00, 0xFF}, 0x0000, 0xFFFF, 0x0000, 0xFFFF}, // Green | ||
{color.RGBA{0x00, 0x00, 0xFF, 0xFF}, 0x0000, 0x0000, 0xFFFF, 0xFFFF}, // Blue | ||
} | ||
for fmt := gopi.SURFACE_FMT_NONE; fmt <= gopi.SURFACE_FMT_MAX; fmt++ { | ||
model := bitmap.GetColorModel(fmt) | ||
if model == nil { | ||
continue | ||
} | ||
if v := model.Format(); v != fmt { | ||
t.Error("Unexpected return from Format()", v) | ||
} else { | ||
t.Log(model) | ||
} | ||
for _, test := range tests { | ||
color := model.Convert(test.c) | ||
r, g, b, a := color.RGBA() | ||
if r != test.r || g != test.g || b != test.b || a != test.a { | ||
t.Error("Unexpected return from Convert()", test.c, " != ", color) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.