Files @ e940c165aae5
Branch filter:

Location: ATITD-Tools/Desert-Paint-Lab/ReactionRecorder.cs - annotation

Jason Maltzen
Fix bar widths on reaction recorder
  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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
2040107278aa
991c95f4da74
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
5d0a31247a93
f28757bb21cb
f2fafa9734f5
e940c165aae5
e940c165aae5
2040107278aa
2040107278aa
2040107278aa
2040107278aa
f2fafa9734f5
f2fafa9734f5
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
532e5f429c0f
532e5f429c0f
532e5f429c0f
532e5f429c0f
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
f28757bb21cb
f28757bb21cb
f28757bb21cb
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f28757bb21cb
f28757bb21cb
991c95f4da74
f28757bb21cb
2040107278aa
5d0a31247a93
2040107278aa
2040107278aa
2040107278aa
2040107278aa
f2fafa9734f5
991c95f4da74
2040107278aa
2040107278aa
991c95f4da74
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
991c95f4da74
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
991c95f4da74
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
991c95f4da74
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
5d0a31247a93
eee2a6a0c86b
eee2a6a0c86b
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
5d0a31247a93
eee2a6a0c86b
eee2a6a0c86b
eee2a6a0c86b
eee2a6a0c86b
5d0a31247a93
eee2a6a0c86b
eee2a6a0c86b
eee2a6a0c86b
5d0a31247a93
eee2a6a0c86b
eee2a6a0c86b
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
eee2a6a0c86b
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
991c95f4da74
f2fafa9734f5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
f2fafa9734f5
f2fafa9734f5
991c95f4da74
991c95f4da74
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
f2fafa9734f5
e940c165aae5
e940c165aae5
e940c165aae5
be9989c2c66e
e940c165aae5
be9989c2c66e
f2fafa9734f5
eee2a6a0c86b
be9989c2c66e
eee2a6a0c86b
991c95f4da74
f2fafa9734f5
f2fafa9734f5
be9989c2c66e
be9989c2c66e
e940c165aae5
e940c165aae5
e940c165aae5
be9989c2c66e
e940c165aae5
be9989c2c66e
be9989c2c66e
e940c165aae5
be9989c2c66e
e940c165aae5
e940c165aae5
e940c165aae5
be9989c2c66e
be9989c2c66e
be9989c2c66e
f2fafa9734f5
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
991c95f4da74
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
f2fafa9734f5
f2fafa9734f5
5d0a31247a93
5d0a31247a93
5d0a31247a93
e940c165aae5
e940c165aae5
532e5f429c0f
532e5f429c0f
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
532e5f429c0f
532e5f429c0f
991c95f4da74
2040107278aa
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
532e5f429c0f
532e5f429c0f
f28757bb21cb
f28757bb21cb
f2fafa9734f5
f28757bb21cb
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
e940c165aae5
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
991c95f4da74
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
f28757bb21cb
/*
 * Copyright (c) 2015, Jason Maltzen

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

using System;
using System.IO;

namespace DesertPaintLab
{
    // ReactionRecorder - business logic for recording paint reactions
    public class ReactionRecorder
    {
        const int COLOR_TOLERANCE = 3;

        const int DEFAULT_SWATCH_HEIGHT = 24; // including top and bottom borders
        const int DEFAULT_SWATCH_WIDTH = 274;
        const int DEFAULT_COLOR_BAR_WIDTH = 320;
        const int DEFAULT_RED_BAR_SPACING = 32;
        const int DEFAULT_GREEN_BAR_SPACING = 42;
        const int DEFAULT_BLUE_BAR_SPACING = 52;

        const int DEFAULT_SWATCH_TEST_WIDTH = 26; // width to test on ends of swatch (10% on either end)

        int swatchHeight = DEFAULT_SWATCH_HEIGHT;
        int swatchWidth = DEFAULT_SWATCH_WIDTH;
        int swatchTestWidth = DEFAULT_SWATCH_TEST_WIDTH;
        int colorBarWidth = DEFAULT_COLOR_BAR_WIDTH;
        int redBarSpacing = DEFAULT_RED_BAR_SPACING;
        int greenBarSpacing = DEFAULT_GREEN_BAR_SPACING;
        int blueBarSpacing = DEFAULT_BLUE_BAR_SPACING;
        int pixelMultiplier = 1;

        bool firstRun = true;
        int lastSwatchX = -1;
        int lastSwatchY = -1;

        private static ReactionRecorder _instance;
        public static ReactionRecorder Instance
        {
            get {
                if (_instance == null)
                {
                    _instance = new ReactionRecorder();
                }
                return _instance;
            }
        }

        private StreamWriter _log;
        public StreamWriter Log
        {
            set
            {
                _log = value;
            }
        }
        private void WriteLog(string format, params object[] args)
        {
            if (_log != null)
            {
                _log.WriteLine(format, args);
            }
        }

        public ReactionRecorder()
        {
            this.pixelMultiplier = 1;
            swatchHeight = DEFAULT_SWATCH_HEIGHT * pixelMultiplier;
            swatchWidth = DEFAULT_SWATCH_WIDTH * pixelMultiplier;
            colorBarWidth = DEFAULT_COLOR_BAR_WIDTH * pixelMultiplier;
            redBarSpacing = DEFAULT_RED_BAR_SPACING * pixelMultiplier;
            greenBarSpacing = DEFAULT_GREEN_BAR_SPACING * pixelMultiplier;
            blueBarSpacing = DEFAULT_BLUE_BAR_SPACING * pixelMultiplier;
            swatchTestWidth = DEFAULT_SWATCH_TEST_WIDTH * pixelMultiplier;
        }

        public ReactionRecorder(int pixelMultiplier)
        {
            this.pixelMultiplier = pixelMultiplier;
            swatchHeight = DEFAULT_SWATCH_HEIGHT * pixelMultiplier;
            swatchWidth = DEFAULT_SWATCH_WIDTH * pixelMultiplier;
            colorBarWidth = DEFAULT_COLOR_BAR_WIDTH * pixelMultiplier;
            redBarSpacing = DEFAULT_RED_BAR_SPACING * pixelMultiplier;
            greenBarSpacing = DEFAULT_GREEN_BAR_SPACING * pixelMultiplier;
            blueBarSpacing = DEFAULT_BLUE_BAR_SPACING * pixelMultiplier;
            swatchTestWidth = DEFAULT_SWATCH_TEST_WIDTH * pixelMultiplier;
        }

        private static bool IsPapyTexture(byte r, byte g, byte b)
        {
            // red between 208 and 244
            // green between 192 and 237
            // blue between 145 and 205
            return ((r > 0xD0) && (g >= 0xC0) && (b >= 0x91)) &&
                   ((r < 0xF4) && (g <= 0xED) && (b <= 0xCD));
        }

        public void SetPixelMultiplier(int pixelMultiplier)
        {
            swatchHeight    = DEFAULT_SWATCH_HEIGHT * pixelMultiplier;
            swatchWidth     = DEFAULT_SWATCH_WIDTH * pixelMultiplier;
            colorBarWidth   = DEFAULT_COLOR_BAR_WIDTH * pixelMultiplier;
            redBarSpacing   = DEFAULT_RED_BAR_SPACING * pixelMultiplier;
            greenBarSpacing = DEFAULT_GREEN_BAR_SPACING * pixelMultiplier;
            blueBarSpacing  = DEFAULT_BLUE_BAR_SPACING * pixelMultiplier;
            swatchTestWidth = DEFAULT_SWATCH_TEST_WIDTH * pixelMultiplier;
            this.pixelMultiplier = pixelMultiplier;
        }

        unsafe private void ColorAt(byte *pixBytes, int x, int y, int stride, out byte r, out byte g, out byte b)
        {
            int pixelStart = (y * stride) + (x * 3);
                    r = pixBytes[pixelStart];
                    g = pixBytes[pixelStart + 1];
                    b = pixBytes[pixelStart + 2];
        }

        private bool IsDarkPixel(int r, int g, int b)
        {
            return ((r < 0x46) && (g < 0x46) && (b < 0x47));
        }

        private bool IsColorMatch(byte test_r, byte test_g, byte test_b, byte target_r, byte target_g, byte target_b)
        {
            return ((Math.Abs(test_r - target_r) <= COLOR_TOLERANCE) &&
                (Math.Abs(test_g - target_g) <= COLOR_TOLERANCE) &&
                (Math.Abs(test_b - target_b) <= COLOR_TOLERANCE));
        }

        unsafe private bool IsPossibleSwatchSlice(byte* pixBytes, int x, int y, int stride)
        {
            int testPixelStart = (y * stride) + (x * 3);
            byte r = pixBytes[testPixelStart];
            byte g = pixBytes[testPixelStart+1];
            byte b = pixBytes[testPixelStart+2];

            bool isDarkPixel = false;
            //bool isPapyAbove = false;
            //bool isPapyBelow = false;

            // 1.) Check if the top pixel is a dark pixel.
            isDarkPixel = IsDarkPixel(r, g, b); // ((r < 0x46) && (g < 0x46) && (b < 0x46));
            
            //// 2.) Check the pixel above it to see if it's from the papy texture.
            //int otherPixelStart = testPixelStart - stride;
            //isPapyAbove = ((otherPixelStart >= 0) &&
            //    IsPapyTexture(pixBytes[otherPixelStart++], pixBytes[otherPixelStart++], pixBytes[otherPixelStart]));

            //// 3.) Check the pixel below where the swatch should be to see if it's also from the papy texture.
            //otherPixelStart = testPixelStart + (stride * swatchHeight);
            //isPapyBelow = (IsPapyTexture(pixBytes[otherPixelStart++], pixBytes[otherPixelStart++], pixBytes[otherPixelStart]));

            //bool result = isDarkPixel && isPapyAbove && isPapyBelow;
            bool result = isDarkPixel;

            // grab the swatch color
            int swatchColorStart = testPixelStart + (2 * pixelMultiplier * stride); // 2 rows below the test pixel, skipping the faded color
            r = pixBytes[swatchColorStart];
            g = pixBytes[swatchColorStart+1];
            b = pixBytes[swatchColorStart+2];

            // scan the column from 2 below the top to 2 above the bottom to ensure the color matches
            for (int i = (2*pixelMultiplier); result && (i < swatchHeight-(2*pixelMultiplier)); ++i)
            {
                int otherPixelStart = testPixelStart + (stride * i);
                result &= IsColorMatch(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2], r, g, b);
            }

            if (!result)
            {
                WriteLog("Swatch slice at {0}, {1} failed to match", x, y);
            }

            return result;
        }

        unsafe private bool IsPossibleSwatchUpperLeft(byte* pixBytes, int x, int y, int stride)
        {
            int testPixelStart = (y * stride) + (x * 3);

            if (testPixelStart < stride)
            {
                return false;
            }

            bool result = true;
            // test the left edge for dark pixels
            int i = 0;
            for (i = 0; result && (i < swatchHeight-pixelMultiplier); ++i)
            {
                int otherPixelStart = testPixelStart + (stride * i);
                result &= IsDarkPixel(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]);
            }
            if (!result)
            {
                // No dark border on the left side
                // WriteLog("Failed to find left border for swatch at {0}, {1}", x, y);
                return false;
            }

            // test the dark top border and for papyrus above and below the swatch
            bool borderError = false;
            int papyErrorCount = 0;
            for (i = 0; result && (i < swatchWidth); ++i)
            {
                int otherPixelStart = testPixelStart + (3 * i);
                bool isBorder = IsDarkPixel(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]);
                result &= isBorder;
                if (!isBorder)
                {
                    borderError = true;
                }
                if (otherPixelStart >= stride)
                {
                    otherPixelStart = otherPixelStart - stride;
                    papyErrorCount += (IsPapyTexture(pixBytes[otherPixelStart], pixBytes[otherPixelStart + 1], pixBytes[otherPixelStart + 2]) ? 0 : 1);
                }
                else
                {
                    papyErrorCount++;
                }

                // Checking along the bottom of the swatch - 
                otherPixelStart = testPixelStart + (stride * swatchHeight) + (3 * i);
                papyErrorCount += (IsPapyTexture(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]) ? 0 : 1);
            }

            result &= (papyErrorCount < (swatchWidth / 20)); // allow up to 5% error rate checking for papy texture, because this seems to be inconsistent
            if (!result && ((i > (swatchWidth*0.8)) || (papyErrorCount >= (swatchWidth/20))))
            {
                if (!borderError && (papyErrorCount < swatchWidth))
                {
                    WriteLog("Found a potential swatch candidate of width {0} at {1},{2} that had {3} failures matching papyrus texture", i, x, y, papyErrorCount);
                }
            }

            return result;
        }

        unsafe private bool TestPosition(int x, int y, byte* pixBytes, int screenshotWidth, int screenshotHeight, int stride, ref PaintColor reactedColor, ref int redPixelStart)
        {
            byte pixel_r, pixel_g, pixel_b;
            int pixelStart, otherPixelStart;
            bool colorMatch = true;

            // Look for the color swatch.
            pixelStart = (y * stride) + (x * 3);
            pixel_r = pixBytes[pixelStart];
            pixel_g = pixBytes[pixelStart + 1];
            pixel_b = pixBytes[pixelStart + 2];

            bool foundSwatch = IsPossibleSwatchUpperLeft(pixBytes, x, y, stride); // ((pixel_r < 0x46) && (pixel_g < 0x46) && (pixel_b < 0x46));
            if (foundSwatch)
            {
                int borderXOffset = 0;
                for (borderXOffset = (2 * pixelMultiplier); foundSwatch && (borderXOffset < swatchTestWidth); ++borderXOffset)
                {
                    foundSwatch &= IsPossibleSwatchSlice(pixBytes, x + borderXOffset, y, stride);
                    foundSwatch &= IsPossibleSwatchSlice(pixBytes, x + swatchWidth - borderXOffset, y, stride);
                }
            }

            if (foundSwatch)
            {
                // found a swatch with an appropriate dark top border with papyrus texture above it and papyrus a jump below it
                // 4.) Scan the left border of the potential swatch
                // location.
                colorMatch = true;
                for (int i = 2; i < swatchHeight - (2 * pixelMultiplier); ++i)
                {
                    otherPixelStart = pixelStart + (stride * i);
                    if (!IsColorMatch(pixel_r, pixel_g, pixel_b, pixBytes[otherPixelStart], pixBytes[otherPixelStart + 1], pixBytes[otherPixelStart + 2]))
                    {
                        colorMatch = false;
                        break;
                    }
                }

                if (colorMatch)
                {
                    // WE FOUND THE SWATCH!
                    // Now we know where the color bars are.
                    redPixelStart = pixelStart + (redBarSpacing * stride);
                    int redPixelCount = 0;
                    while ((pixBytes[redPixelStart] > 0x9F) &&
                           (pixBytes[redPixelStart + 1] < 0x62) &&
                           (pixBytes[redPixelStart + 2] < 0x62))
                    {
                        redPixelCount++;
                        // pixBytes[redPixelStart] = 0x00;
                        // pixBytes[redPixelStart + 1] = 0xFF;
                        // pixBytes[redPixelStart + 2] = 0xFF;
                        redPixelStart += 3;
                    }
                    WriteLog("Color {0}, {1}, {2} is no longer red at {3},{4}", pixBytes[redPixelStart], pixBytes[redPixelStart + 1], pixBytes[redPixelStart + 2], (redPixelStart % stride)/3, redPixelStart / stride);

                    reactedColor.Red = (byte)Math.Round((float)redPixelCount * 255f / (float)colorBarWidth);
                    int greenPixelStart = pixelStart + (greenBarSpacing * stride);

                    int greenPixelCount = 0;
                    while ((pixBytes[greenPixelStart] < 0x62) &&
                           (pixBytes[greenPixelStart + 1] > 0x9F) &&
                           (pixBytes[greenPixelStart + 2] < 0x62))
                    {
                        greenPixelCount++;
                        // pixBytes[greenPixelStart] = 0x00;
                        // pixBytes[greenPixelStart + 1] = 0xFF;
                        // pixBytes[greenPixelStart + 2] = 0xFF;
                        greenPixelStart += 3;
                    }
                    WriteLog("Color {0}, {1}, {2} is no longer green at pixel offset {3},{4}", pixBytes[greenPixelStart], pixBytes[greenPixelStart + 1], pixBytes[greenPixelStart + 2], (greenPixelStart % stride)/3, redPixelStart / stride);

                    reactedColor.Green = (byte)Math.Round((float)greenPixelCount * 255f / (float)colorBarWidth);
                    int bluePixelStart = pixelStart + (blueBarSpacing * stride);

                    int bluePixelCount = 0;
                    while ((pixBytes[bluePixelStart] < 0x62) &&
                        (pixBytes[bluePixelStart + 1] < 0x62) &&
                        (pixBytes[bluePixelStart + 2] > 0x9F))
                    {
                        bluePixelCount++;
                        // pixBytes[bluePixelStart] = 0x00;
                        // pixBytes[bluePixelStart + 1] = 0xFF;
                        // pixBytes[bluePixelStart + 2] = 0xFF;
                        bluePixelStart += 3;
                    }
                    WriteLog("Color {0}, {1}, {2} is no longer blue at pixel offset {3},{4}", pixBytes[bluePixelStart], pixBytes[bluePixelStart + 1], pixBytes[bluePixelStart + 2], (bluePixelStart % stride)/3, bluePixelStart / stride);

                    reactedColor.Blue = (byte)Math.Round((float)bluePixelCount * 255f / (float)colorBarWidth);
                    WriteLog("Found the color swatch at {0}, {1}. Color={2} Red={3}px Green={4}px Blue={5}px", x, y, reactedColor, redPixelCount, greenPixelCount, bluePixelCount);
                    return true;
                }
            }
            return false;
        }

        unsafe public bool CaptureReaction(byte* pixBytes, int screenshotWidth, int screenshotHeight, int stride, ref PaintColor reactedColor, ref int redPixelStart)
        {
            if (!firstRun)
            {
                // If this is not the first run, let's check the last location, to see if the UI is still there.
                if (TestPosition(lastSwatchX, lastSwatchY, pixBytes, screenshotWidth, screenshotHeight, stride, ref reactedColor, ref redPixelStart))
                {
                    return true;
                }
                else
                {
                    firstRun = true;
                }
            }

            for (int x = 0; x < screenshotWidth - colorBarWidth; ++x)
            {
                for (int y = 0; y < (screenshotHeight - (blueBarSpacing + 1)  /*53*/); ++y)
                {
                    if (TestPosition(x, y, pixBytes, screenshotWidth, screenshotHeight, stride, ref reactedColor, ref redPixelStart))
                    {
                        lastSwatchX = x;
                        lastSwatchY = y;
                        firstRun = false;
                        return true;
                    }
                }
            }
            return false;
        }

        public bool RecordReaction(PlayerProfile profile, PaintColor expectedColor, PaintColor reactedColor, Reagent[] reagents)
        {
            bool saved = false;
            int r, g, b;
            if (reagents[2] != null)
            {
                // A 3-reagent reaction.
                Reaction reaction1 = profile.FindReaction(reagents[0], reagents[1]);
                Reaction reaction2 = profile.FindReaction(reagents[0], reagents[2]);
                Reaction reaction3 = profile.FindReaction(reagents[1], reagents[2]);
                
                r = reactedColor.Red - expectedColor.Red;
                g = reactedColor.Green - expectedColor.Green;
                b = reactedColor.Blue - expectedColor.Blue;
                
                if (reaction2 == null)
                {
                    r = r - reaction1.Red - reaction3.Red;
                    g = g - reaction1.Green - reaction3.Green;
                    b = b - reaction1.Blue - reaction3.Blue;
                    profile.SetReaction(reagents[0], reagents[2], new Reaction(r, g, b));
                    profile.Save();
                    saved = true;
                }
                else if (reaction3 == null)
                {
                    r = r - reaction1.Red - reaction2.Red;
                    g = g - reaction1.Green - reaction2.Green;
                    b = b - reaction1.Blue - reaction2.Blue;
                    profile.SetReaction(reagents[1], reagents[2], new Reaction(r, g, b));
                    profile.Save();
                    saved = true;
                }   
            }
            else if ((reagents[0] != null) && (reagents[1] != null))
            {
                // A 2-reagent reaction.
                r = reactedColor.Red - expectedColor.Red;
                g = reactedColor.Green - expectedColor.Green;
                b = reactedColor.Blue - expectedColor.Blue;
                profile.SetReaction(reagents[0], reagents[1], new Reaction(r, g, b));
                profile.Save();
                saved = true;
            }
            return saved;
        }
    }
}