Changeset - 334157513064
[Not reviewed]
default
0 1 0
Jason Maltzen - 5 years ago 2019-11-08 21:35:30
jason@hiddenachievement.com
Change IsRed / IsGreen / IsBlue tests to be a little more tolerant of brighter pixels - was not detecting green clipped high correctly with the new brighter background texture.
1 file changed with 3 insertions and 3 deletions:
0 comments (0 inline, 0 general)
ReactionRecorder.cs
Show inline comments
...
 
@@ -75,265 +75,265 @@ namespace DesertPaintLab
 
        public static readonly int[] RED_BAR_SPACING =
 
        {
 
            32, // tiny
 
            32, // small
 
            32, // medium
 
            32, // large
 
            32 // huge
 
        };
 
        public static readonly int[] GREEN_BAR_SPACING =
 
        {
 
            42, // tiny
 
            42, // small
 
            42, // medium
 
            42, // large
 
            42 // huge
 
        };
 
        public static readonly int[] BLUE_BAR_SPACING =
 
        {
 
            52, // tiny
 
            52, // small
 
            52, // medium
 
            52, // large
 
            52 // huge
 
        };
 
        // width to test on ends of swatch (10% on either end)
 
        public static readonly int[] SWATCH_TEST_WIDTH =
 
        {
 
            26, // tiny
 
            26, // small
 
            26, // medium
 
            28, // large
 
            31  // huge
 
        };
 

	
 
        int swatchHeight = SWATCH_HEIGHT[(int)DEFAULT_INTERFACE_SIZE];
 
        int swatchWidth = SWATCH_WIDTH[(int)DEFAULT_INTERFACE_SIZE];
 
        int swatchTestWidth = SWATCH_TEST_WIDTH[(int)DEFAULT_INTERFACE_SIZE];
 
        int colorBarWidth = COLOR_BAR_WIDTH[(int)DEFAULT_INTERFACE_SIZE];
 
        int redBarSpacing = RED_BAR_SPACING[(int)DEFAULT_INTERFACE_SIZE];
 
        int greenBarSpacing = GREEN_BAR_SPACING[(int)DEFAULT_INTERFACE_SIZE];
 
        int blueBarSpacing = BLUE_BAR_SPACING[(int)DEFAULT_INTERFACE_SIZE];
 

	
 
        public int SwatchHeight {  get { return swatchHeight; } }
 
        public int SwatchWidth {  get { return swatchWidth; } }
 
        public int ColorBarWidth { get { return colorBarWidth;  } }
 
        public int RedBarSpacing {  get { return redBarSpacing; } }
 
        public int GreenBarSpacing { get { return greenBarSpacing; } }
 
        public int BlueBarSpacing {  get { return blueBarSpacing; } }
 

	
 
        // Current Status
 
        public bool IsCaptured { get; private set; }
 
        public int X { get; private set; }
 
        public int Y { get; private set; }
 
        public int ScreenWidth { get; private set; }
 
        public int ScreenHeight { get; private set; }
 
        private PaintColor _recordedColor = new PaintColor();
 
        public PaintColor RecordedColor { get { return _recordedColor; } private set { _recordedColor.Set(value); } }
 
        public int RedBarX { get; private set; }
 
        public int RedBarY { get; private set; }
 

	
 
        int pixelMultiplier = 1;
 
        public int PixelMultiplier
 
        {
 
            get
 
            {
 
                return pixelMultiplier;
 
            }
 
            set
 
            {
 
                pixelMultiplier = value;
 
                UpdateSwatchSizes();
 
            }
 
        }
 
        private InterfaceSize interfaceSize;
 
        public InterfaceSize InterfaceSize
 
        {
 
            get
 
            {
 
                return interfaceSize;
 
            }
 
            set
 
            {
 
                interfaceSize = value;
 
                UpdateSwatchSizes();
 
            }
 
        }
 

	
 
        bool firstRun = true;
 
        int lastSwatchX = -1;
 
        int lastSwatchY = -1;
 
        public int SwatchX { get { return lastSwatchX; } }
 
        public int SwatchY { get { return lastSwatchY; } }
 

	
 
        private LogVerbosity logVerbosity = LogVerbosity.Normal;
 

	
 
        private class PixelColor
 
        {
 
            public byte r;
 
            public byte g;
 
            public byte b;
 

	
 
            public bool IsMatch(PixelColor other)
 
            {
 
                return ((Math.Abs(r - other.r) <= COLOR_TOLERANCE) &&
 
                    (Math.Abs(g - other.g) <= COLOR_TOLERANCE) &&
 
                    (Math.Abs(b - other.b) <= COLOR_TOLERANCE));
 
            }
 

	
 
            public static bool IsDark(PixelColor color)
 
            {
 
                return (color.r < 0x47) && (color.g < 0x47) && (color.b < 0x47);
 
            }
 

	
 
            public static bool IsPapyrus(PixelColor color)
 
            {
 
                // red between 208 and 244
 
                // 240 and 255
 
                // green between 192 and 237
 
                // 223 and 250
 
                // blue between 145 and 205
 
                // 178 and 232
 
                //return ((r > 0xD0) && (g >= 0xC0) && (b >= 0x91)) &&
 
                //       ((r < 0xF4) && (g <= 0xED) && (b <= 0xCD));
 
                return ((color.r >= 0xF0) && (color.r <= 0xFF) && (color.g >= 0xDF) && (color.g <= 0xFA) && (color.b >= 0xB2) && (color.b <= 0xE8));
 
            }
 

	
 
            public static bool IsRed(PixelColor color)
 
            {
 
                return (color.r > 0x9F) && (color.g < 0x62) && (color.b < 0x62);
 
                return (color.r > 0x9F) && (color.g < 0x70) && (color.b < 0x70);
 
            }
 
            public static bool IsGreen(PixelColor color)
 
            {
 
                return (color.r < 0x62) && (color.g > 0x9F) && (color.b < 0x62);
 
                return (color.r < 0x70) && (color.g > 0x9F) && (color.b < 0x70);
 
            }
 
            public static bool IsBlue(PixelColor color)
 
            {
 
                return (color.r < 0x62) && (color.g < 0x62) && (color.b > 0x9F);
 
                return (color.r < 0x70) && (color.g < 0x70) && (color.b > 0x9F);
 
            }
 
        }
 

	
 
        unsafe private class Pixels
 
        {
 
            public byte* pixBytes;
 
            public int width;
 
            public int height;
 
            public int stride;
 
            public int pixelMultiplier;
 
            public int numChannels;
 

	
 
            private PixelColor _tempColor = new PixelColor();
 
            private PixelColor _tempColor2 = new PixelColor();
 

	
 
            public int ComputeOffset(int x, int y)
 
            {
 
                return (y * pixelMultiplier * stride) + (x * pixelMultiplier * numChannels);
 
            }
 

	
 
            public int UpdateOffset(int offset, int xChange, int yChange)
 
            {
 
                return offset + (yChange * pixelMultiplier * stride) + (xChange * pixelMultiplier * numChannels);
 
            }
 

	
 
            unsafe public void ColorAt(int offset, ref PixelColor pixel)
 
            {
 
                pixel.r = pixBytes[offset];
 
                pixel.g = pixBytes[offset + 1];
 
                pixel.b = pixBytes[offset + 2];
 
            }
 

	
 
            unsafe public void ColorAt(int x, int y, ref PixelColor pixel)
 
            {
 
                int offset = ComputeOffset(x, y);
 
                ColorAt(offset, ref pixel);
 
            }
 

	
 
            unsafe public bool DoesPixelMatch(int x, int y, Func<PixelColor, bool> matchFunc)
 
            {
 
                int offset = ComputeOffset(x, y);
 
                ColorAt(offset, ref _tempColor);
 
                return matchFunc(_tempColor);
 
            }
 

	
 
            unsafe public bool DoesPixelMatch(int offset, Func<PixelColor, bool> matchFunc)
 
            {
 
                ColorAt(offset, ref _tempColor);
 
                return matchFunc(_tempColor);
 
            }
 

	
 
            // Compute length of horizontal bar starting at x,y using matching function
 
            unsafe public int LengthOfColorAt(int x, int y, Func<PixelColor, bool> matchesColor)
 
            {
 
                int count = 0;
 
                for (int xVal = x; xVal < width; ++xVal)
 
                {
 
                    int offset = ComputeOffset(xVal, y);
 
                    if (!DoesPixelMatch(xVal, y, matchesColor)) break;
 
                    ++count;
 
                }
 
                return count;
 
            }
 

	
 
            unsafe public bool IsSolidPatchAt(int x, int y, int patchWidth, int patchHeight)
 
            {
 
                if ((x + patchWidth >= width) || (y + patchHeight >= height)) return false;
 
                ColorAt(x, y, ref _tempColor2);
 
                Console.WriteLine("color at {0},{1} = {2},{3},{4}", x, y, _tempColor2.r, _tempColor2.g, _tempColor2.b);
 
                bool ok = true;
 
                ok &= DoesPixelMatch(x + patchWidth - 1, y, _tempColor2.IsMatch);
 
                ok &= DoesPixelMatch(x, y + patchHeight - 1, _tempColor2.IsMatch);
 
                ok &= DoesPixelMatch(x + patchWidth - 1, y + patchHeight - 1, _tempColor2.IsMatch);
 
                for (int xOff = 1; ok && (xOff < patchWidth - 1); ++xOff)
 
                {
 
                    ok &= DoesPixelMatch(x + xOff, y, _tempColor2.IsMatch);
 
                    ok &= DoesPixelMatch(x + xOff, y + patchHeight - 1, _tempColor2.IsMatch);
 
                }
 
                for (int yOff = 1; ok && (yOff < patchHeight - 1); ++yOff)
 
                {
 
                    ok &= DoesPixelMatch(x, y + yOff, _tempColor2.IsMatch);
 
                    ok &= DoesPixelMatch(x + patchWidth - 1, y + yOff, _tempColor2.IsMatch);
 
                }
 
                for (int yOff = 1; ok && (yOff < patchHeight - 1); ++yOff)
 
                {
 
                    for (int xOff = 1; ok && (xOff < patchWidth - 1); ++xOff)
 
                    {
 
                        ok &= DoesPixelMatch(x + xOff, y + yOff, _tempColor2.IsMatch);
 
                    }
 
                }
 
                return ok;
 
            }
 
        }
 

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

	
 
        public StreamWriter Log { get; set; }
 
        private void WriteLog(LogVerbosity verbosity, string format, params object[] args)
 
        {
 
            if ((Log != null) && (verbosity <= logVerbosity))
 
            {
 
                Log.WriteLine(format, args);
 
            }
 
        }
 

	
 
        public delegate void ReactionRecordedHandler(Reagent reagent1, Reagent reagent2, Reaction reaction);
 
        public ReactionRecordedHandler OnReactionRecorded;
 

	
 
        public ReactionRecorder()
 
        {
 
            pixelMultiplier = 1;
 
            interfaceSize = DEFAULT_INTERFACE_SIZE;
 

	
 
            UpdateSwatchSizes();
 
        }
 

	
 
        public ReactionRecorder(int pixelMultiplier)
 
        {
0 comments (0 inline, 0 general)