ketmar patch for color convenience

This commit is contained in:
Adam D. Ruppe 2016-04-26 11:22:03 -04:00
parent 45ba42f711
commit 8255d3c2d0
1 changed files with 32 additions and 1 deletions

33
color.d
View File

@ -198,6 +198,13 @@ struct Color {
}
*/
/// Return black-and-white color
Color toBW() () {
int intens = cast(int)(0.2126*r+0.7152*g+0.0722*b);
if (intens < 0) intens = 0; else if (intens > 255) intens = 255;
return Color(intens, intens, intens, a);
}
/// Makes a string that matches CSS syntax for websites
string toCssString() {
if(a == 255)
@ -767,6 +774,9 @@ interface MemoryImage {
/// Get image pixel. Slow, but returns valid RGBA color (completely transparent for off-image pixels).
Color getPixel(int x, int y) const;
// Set image pixel.
void setPixel(int x, int y, in Color clr);
}
/// An image that consists of indexes into a color palette. Use getAsTrueColorImage() if you don't care about palettes
@ -800,6 +810,20 @@ class IndexedImage : MemoryImage {
}
}
override void setPixel(int x, int y, in Color clr) @trusted {
if (x >= 0 && y >= 0 && x < _width && y < _height) {
uint pos = y*_width+x;
if (pos >= data.length) return;
ubyte pidx = findNearestColor(palette, clr);
if (palette.length < 255 &&
(palette.ptr[pidx].r != clr.r || palette.ptr[pidx].g != clr.g || palette.ptr[pidx].b != clr.b || palette.ptr[pidx].a != clr.a)) {
// add new color
pidx = addColor(clr);
}
data.ptr[pos] = pidx;
}
}
private int _width;
private int _height;
@ -901,13 +925,20 @@ class TrueColorImage : MemoryImage {
override Color getPixel(int x, int y) const @trusted {
if (x >= 0 && y >= 0 && x < _width && y < _height) {
uint pos = y*_width+x;
if (pos >= imageData.bytes.length/4) return Color(0, 0, 0, 0);
if (pos+3 >= imageData.bytes.length/4) return Color(0, 0, 0, 0);
return imageData.colors.ptr[pos];
} else {
return Color(0, 0, 0, 0);
}
}
override void setPixel(int x, int y, in Color clr) @trusted {
if (x >= 0 && y >= 0 && x < _width && y < _height) {
uint pos = y*_width+x;
if (pos+3 < imageData.bytes.length/4) imageData.colors.ptr[pos] = clr;
}
}
/// .
this(int w, int h) {
_width = w;