1 module rip.processing.grayscale;
2 
3 private
4 {
5 	import std.algorithm;
6 	import std.range;
7 
8 	import rip.concepts.ranges;
9 	import rip.concepts.templates;
10 	import rip.concepts.color;
11 	import rip.concepts.surface;
12 }
13 
14 enum GrayPalette
15 {
16 	STANDART,
17 	LUMINANCE,
18 	AVERAGE
19 }
20 
21 class Grayscale {
22 	GrayPalette palette;
23 
24 	RGBColor getNewColor(RGBColor a) {
25 		float intensity;
26 
27 		final switch (palette) with (GrayPalette)
28 		{
29 			case STANDART:
30 				intensity = 0.2126 * a.red!float + 0.7152 * a.green!float + 0.0722 * a.blue!float;
31 			break;
32 
33 			case LUMINANCE:
34 				intensity = a.luminance!float;
35 			break;
36 
37 			case AVERAGE:
38 				intensity = (a.red!float + a.green!float + a.blue!float) / 3.0;
39 			break;
40 
41 		}
42 		return new RGBColor(intensity, intensity, intensity);
43 	}
44 
45 	void 	refColor(ref RGBColor a) {
46 		a = null;
47 	}
48 
49 	this(GrayPalette palette) {
50 		this.palette = palette;
51 	}
52 }
53 
54 /*auto toGrayScale(Range)(Range r, GrayPalette palette = GrayPalette.STANDART)
55 	if(isPixelRange!Range)
56 {
57 	RGBColor delegate(RGBColor) grayFunction;
58 
59 	final switch (palette) with (GrayPalette)
60 	{
61 		case STANDART:
62 			grayFunction = delegate(RGBColor color) {
63 				auto intensity = ((RGBColor color) => 0.2126 * color.red!float + 0.7152 * color.green!float + 0.0722 * color.blue!float);
64 				return new RGBColor(intensity(color), intensity(color), intensity(color));
65 			};
66 			break;
67 
68 		case LUMINANCE:
69 			grayFunction = delegate(RGBColor color) {
70 				auto intensity = color.luminance!float;
71 				return new RGBColor(intensity, intensity, intensity);
72 			};
73 			break;
74 
75 		case AVERAGE:
76 			grayFunction = delegate(RGBColor color) {
77 				auto intensity = (color.red!float + color.green!float + color.blue!float) / 3.0;
78 				return new RGBColor(intensity, intensity, intensity);
79 			};
80 			break;
81 	}
82 	auto range = map!(a => grayFunction(a))(r).array;
83 	return createPixels(range);
84 }*/
85 
86 /*auto toGrayScale(Surface surface, GrayPalette palette = GrayPalette.STANDART)
87 {
88 	auto image = surface
89 		.createFences(1,1)
90 			.map!(a => a.front)
91 			.toGrayScale(palette)
92 			.toSurface(surface.getWidth!int, surface.getHeight!int);
93 	return image;
94 }*/