Permalink
Browse files

Surface: the pixel CTor uses now the Masks

Color4b: Implemented hex conversion in both directions
  • Loading branch information...
Dgame committed Apr 28, 2015
1 parent f159b88 commit 2900fc70c13d53d47de90dc2331fff70b8a4976a
Showing with 64 additions and 19 deletions.
  1. +30 −0 source/Dgame/Graphic/Color.d
  2. +1 −1 source/Dgame/Graphic/Surface.d
  3. +15 −2 source/Dgame/docs/Color.html
  4. +18 −16 source/Dgame/test/main.d
@@ -72,9 +72,23 @@ struct Color4b {
this.blue = blue;
this.alpha = alpha;
}
/**
* CTor
*
* Expect ABGR as format
*/
@nogc
this(uint hexValue) pure nothrow {
this.alpha = (hexValue >> 24) & 0xff;
this.blue = (hexValue >> 16) & 0xff;
this.green = (hexValue >> 8) & 0xff;
this.red = hexValue & 0xff;
}
/**
* CTor
*
* Expect that every component is in range 0.0 .. 1.0
*/
@nogc
@@ -125,6 +139,22 @@ struct Color4b {
ubyte[3] asRGB() const pure nothrow {
return [this.red, this.green, this.blue];
}
/**
* Returns the RGBA color information as hex value (in ABGR format)
*/
@nogc
uint asHex() const pure nothrow {
return ((this.alpha & 0xff) << 24) + ((this.blue & 0xff) << 16) + ((this.green & 0xff) << 8) + (this.red & 0xff);
}
}
unittest {
const Color4b red_col = Color4b.Red;
immutable uint hex_red = red_col.asHex();
assert(hex_red == 0xff0000ff);
assert(Color4b(hex_red) == red_col);
}
/**
@@ -90,7 +90,7 @@ private:
assert(memory, "Memory is empty.");
assert(depth == 8 || depth == 16 || depth == 24 || depth == 32, "Invalid depth.");
return SDL_CreateRGBSurfaceFrom(memory, width, height, depth, (depth / 8) * width, 0, 0, 0, 0);
return SDL_CreateRGBSurfaceFrom(memory, width, height, depth, (depth / 8) * width, RMask, GMask, BMask, AMask);
}
public:
@@ -82,10 +82,18 @@ <h1>Dgame.Graphic.Color</h1>
<dd>CTor<br><br>
</dd>
<dt><big><a name="Color4b.this.2"></a>pure nothrow @nogc this()(auto ref const Color4f <i>col</i>);
<dt><big><a name="Color4b.this.2"></a>pure nothrow @nogc this(uint <i>hexValue</i>);
</big></dt>
<dd>CTor
Expect that every component is in range 0.0 .. 1.0<br><br>
<br><br>
Expect ABGR as format<br><br>
</dd>
<dt><big><a name="Color4b.this.3"></a>pure nothrow @nogc this()(auto ref const Color4f <i>col</i>);
</big></dt>
<dd>CTor
<br><br>
Expect that every component is in range 0.0 .. 1.0<br><br>
</dd>
<dt><big><a name="Color4b.withTransparency"></a>const pure nothrow @nogc Color4b <u>withTransparency</u>(ubyte <i>alpha</i>);
@@ -108,6 +116,11 @@ <h1>Dgame.Graphic.Color</h1>
</big></dt>
<dd>Returns RGB the color information as static array<br><br>
</dd>
<dt><big><a name="Color4b.asHex"></a>const pure nothrow @nogc uint <u>asHex</u>();
</big></dt>
<dd>Returns the RGBA color information as hex value (in ABGR format)<br><br>
</dd>
</dl>
</dd>
View
@@ -31,23 +31,25 @@ void main() {
}
}
*/
immutable uint black = Color4b.Black.asHex();
immutable uint blue = Color4b.Blue.asHex();
uint[256] xpixels = [
255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255,
0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0,
0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0,
0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0,
0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0,
0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0,
0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0,
0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0,
0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0,
0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0,
255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255,
blue, black, black, black, black, black, black, black, black, black, black, black, black, black, black, blue,
black, blue, black, black, black, black, black, black, black, black, black, black, black, black, blue, black,
black, black, blue, black, black, black, black, black, black, black, black, black, black, blue, black, black,
black, black, black, blue, black, black, black, black, black, black, black, black, blue, black, black, black,
black, black, black, black, blue, black, black, black, black, black, black, blue, black, black, black, black,
black, black, black, black, black, blue, black, black, black, black, blue, black, black, black, black, black,
black, black, black, black, black, black, blue, black, black, blue, black, black, black, black, black, black,
black, black, black, black, black, black, black, blue, blue, black, black, black, black, black, black, black,
black, black, black, black, black, black, black, blue, blue, black, black, black, black, black, black, black,
black, black, black, black, black, black, blue, black, black, blue, black, black, black, black, black, black,
black, black, black, black, black, blue, black, black, black, black, blue, black, black, black, black, black,
black, black, black, black, blue, black, black, black, black, black, black, blue, black, black, black, black,
black, black, black, blue, black, black, black, black, black, black, black, black, blue, black, black, black,
black, black, blue, black, black, black, black, black, black, black, black, black, black, blue, black, black,
black, blue, black, black, black, black, black, black, black, black, black, black, black, black, blue, black,
blue, black, black, black, black, black, black, black, black, black, black, black, black, black, black, blue,
];
Surface xs = Surface(xpixels.ptr, 16, 16);

0 comments on commit 2900fc7

Please sign in to comment.