1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252:
| using System; using System.Drawing; using System.Drawing.Imaging;
public class EditColor { private const int MatrixLength = 5; private float[,] mLength = new float[MatrixLength, MatrixLength]; private const float Radius = (float)(Math.PI / 180.0);
public static float lumR = 0.3086f; public static float lumG = 0.6094f; public static float lumB = 0.0820f;
private static EditColor OldHue = new EditColor(); private static EditColor NewHue = new EditColor(); private static bool initialized = false;
public enum MatrixOrder { MatrixOrderPrepend = 0, MatrixOrderAppend = 1 }
public EditColor() { Reset(); }
public EditColor(float[,] m) { if (m == null) { Reset(); return; } Copy(m); }
public EditColor(EditColor qm) { Copy(qm); }
public ColorMatrix ToColorMatrix() { ColorMatrix cm = new ColorMatrix(); for (int i = 0; i < MatrixLength; i++) { for (int j = 0; j < MatrixLength; j++) { cm[i, j] = mLength[i, j]; } } return cm; }
private void Copy(EditColor qm) { if (qm == null) { Reset(); return; } Copy(qm.mLength); }
private void Copy(float[,] m) { if ((m == null) || (m.Length != this.mLength.Length)) { throw new ArgumentException(); } Array.Copy(m, this.mLength, m.Length); }
public void Reset() { for (int i = 0; i < MatrixLength; i++) { for (int j = 0; j < MatrixLength; j++) { mLength[i, j] = ((i == j) ? 1.0f : 0.0f); } } }
public void RotateHue(float phi) { InitHue(); Multiply(OldHue, MatrixOrder.MatrixOrderAppend); RotateBlue(phi, MatrixOrder.MatrixOrderAppend); Multiply(NewHue, MatrixOrder.MatrixOrderAppend); }
private static void InitHue() { const float greenRotation = 35.0f;
if (!initialized) { initialized = true; OldHue.RotateRed(45.0f); OldHue.RotateGreen(-greenRotation, MatrixOrder.MatrixOrderAppend); float[] lum = new float[] { lumR, lumG, lumB, 1.0f }; OldHue.TransformVector(lum); float red = lum[0] / lum[2]; float green = lum[1] / lum[2]; OldHue.ShearBlue(red, green, MatrixOrder.MatrixOrderAppend); NewHue.ShearBlue(-red, -green); NewHue.RotateGreen(greenRotation, MatrixOrder.MatrixOrderAppend); NewHue.RotateRed(-45.0f, MatrixOrder.MatrixOrderAppend); } }
public float[] TransformVector(float[] v) { return TransformVector(v, false); }
public float[] TransformVector(float[] v, bool normalize) { if (v == null || (v.Length < 4)) { throw new ArgumentException(); } float[] temp = new float[4]; for (int x = 0; x < 4; x++) { temp[x] = 255.0f * mLength[4, x]; for (int y = 0; y < 4; y++) { temp[x] += v[y] * mLength[y, x]; } } for (int x = 0; x < 4; x++) { v[x] = temp[x]; if (normalize) { if (v[x] < 0) v[x] = 0.0f; else if (v[x] > 255.0f) v[x] = 255.0f; } } return v; }
public void Multiply(EditColor matrix) { Multiply(matrix, MatrixOrder.MatrixOrderPrepend); }
public void Multiply(EditColor matrix, MatrixOrder order) { if (matrix == null) throw new ArgumentException(); float[,] a = null; float[,] b = null;
if (order == MatrixOrder.MatrixOrderAppend) { a = matrix.mLength; b = mLength; } else { a = mLength; b = matrix.mLength; }
float[,] temp = new float[MatrixLength, MatrixLength]; for (int y = 0; y < MatrixLength; y++) { for (int x = 0; x < MatrixLength; x++) { float t = 0; for (int i = 0; i < MatrixLength; i++) { t += b[y, i] * a[i, x]; } temp[y, x] = t; } } for (int y = 0; y < MatrixLength; y++) { for (int x = 0; x < MatrixLength; x++) { mLength[y, x] = temp[y, x]; } } }
public void RotateRed(float phi) { RotateRed(phi, MatrixOrder.MatrixOrderPrepend); }
public void RotateGreen(float phi) { RotateGreen(phi, MatrixOrder.MatrixOrderPrepend); }
public void RotateBlue(float phi) { RotateBlue(phi, MatrixOrder.MatrixOrderPrepend); }
public void RotateRed(float phi, MatrixOrder order) { RotateColor(phi, 2, 1, order); }
public void RotateGreen(float phi, MatrixOrder order) { RotateColor(phi, 0, 2, order); }
public void RotateBlue(float phi, MatrixOrder order) { RotateColor(phi, 1, 0, order); }
private void RotateColor(float phi, int x, int y, MatrixOrder order) { phi *= Radius; EditColor qm = new EditColor();
qm.mLength[x, x] = qm.mLength[y, y] = (float)Math.Cos(phi);
float s = (float)Math.Sin(phi); qm.mLength[y, x] = s; qm.mLength[x, y] = -s;
Multiply(qm, order); }
public void ShearBlue(float red, float green) { ShearBlue(red, green, MatrixOrder.MatrixOrderPrepend); }
public void ShearBlue(float red, float green, MatrixOrder order) { ShearColor(2, 0, red, 1, green, order); }
private void ShearColor(int x, int y1, float d1, int y2, float d2, MatrixOrder order) { EditColor qm = new EditColor(); qm.mLength[y1, x] = d1; qm.mLength[y2, x] = d2; Multiply(qm, order); } } |