Changeset - dd8780bb11c5
[Not reviewed]
default
0 1 0
Jason Maltzen - 3 years ago 2021-09-08 13:03:28
jason@hiddenachievement.com
Correct an error when computing the clipped value on white shifts. It was incorrectly detecting a white shift on single-color clips, resulting in a bad reaction computation. Also don't display the wrong 'observed' color after clearing a reaction.
1 file changed with 7 insertions and 6 deletions:
0 comments (0 inline, 0 general)
Models/ReactionTest.cs
Show inline comments
...
 
@@ -158,209 +158,210 @@ namespace DesertPaintCodex.Models
 
            Reagent2 = reagent2;
 
            UseFirstBuffer = true;
 
            IsAllCatalysts = reagent1.IsCatalyst && reagent2.IsCatalyst;
 
            Clipped = clipType;
 
            Reaction = reaction;
 
            State = (reaction != null) ? TestState.Saved :
 
                (clipType == ClipType.None) ? TestState.Untested : TestState.ClippedResult;
 
            _recipe.AddReagent(reagent1.Name);
 
            _recipe.AddReagent(reagent2.Name);
 
            IsStub = isStub;
 
            UpdateHypotheticalColor();
 
        }
 

	
 
        
 
        #region Actions
 
        public async Task StartScan()
 
        {
 
            Clipped = ClipType.None;
 
            ScanProgress = 0;
 
            Reaction = null;
 
            BadReaction = null;
 
            State = TestState.Scanning;
 
            bool testing3Way = Requires3Way && BufferIsSelected;
 
            bool foundLab = await ReactionScannerService.Instance.CaptureReactionAsync(this);
 
            if (foundLab)
 
            {
 
                ObservedColor = ReactionScannerService.Instance.RecordedColor;
 
                if (_observedColor != null)
 
                {
 
                    Clipped = _observedColor.Red switch
 
                        {
 
                            0   => ClipType.RedLow,
 
                            255 => ClipType.RedHigh,
 
                            _   => ClipType.None
 
                        }
 
                        | _observedColor.Green switch
 
                        {
 
                            0   => ClipType.GreenLow,
 
                            255 => ClipType.GreenHigh,
 
                            _   => ClipType.None
 
                        }
 
                        | _observedColor.Blue switch
 
                        {
 
                            0   => ClipType.BlueLow,
 
                            255 => ClipType.BlueHigh,
 
                            _   => ClipType.None
 
                        };
 

	
 
                    if (Clipped == ClipType.None)
 
                    {
 
                        State = TestState.GoodResult;
 
                        Reaction = CalculateReaction();
 
                    }
 
                    else
 
                    {
 
                        Reaction? reaction = CalculateReaction();
 
                        bool extrapolated = false;
 
                        
 
                        // SPECIAL CASE:
 
                        // Check to see if we've got a white-shift reaction that's partially clipped, where we can
 
                        // still extrapolate the reaction, based on the available information.
 
                        if (!testing3Way && (reaction != null))
 
                        {
 
                            PaintColor baseColor = _recipe.BaseColor;
 
                            if ((reaction.Red < baseColor.Red) &&
 
                                (reaction.Green < baseColor.Green) &&
 
                                (reaction.Blue < baseColor.Blue))
 
                            if ((reaction.Red < 0) &&
 
                                (reaction.Green < 0) &&
 
                                (reaction.Blue < 0))
 
                            {
 
                                // White-shift down clip.
 
                                extrapolated                    = ExtrapolateWhiteFromOneChannel(_observedColor.Red,   0, reaction.Red);
 
                                if (!extrapolated) extrapolated = ExtrapolateWhiteFromOneChannel(_observedColor.Green, 0, reaction.Green);
 
                                if (!extrapolated)                ExtrapolateWhiteFromOneChannel(_observedColor.Blue,  0, reaction.Blue);
 
                            }
 
                            else if ((reaction.Red > baseColor.Red) &&
 
                                (reaction.Green > baseColor.Green) &&
 
                                (reaction.Blue > baseColor.Blue))
 
                            else if ((reaction.Red > 0) &&
 
                                (reaction.Green > 0) &&
 
                                (reaction.Blue > 0))
 
                            {
 
                                // White-shift up clip.
 
                                extrapolated                    = ExtrapolateWhiteFromOneChannel(_observedColor.Red,   255, reaction.Red);
 
                                if (!extrapolated) extrapolated = ExtrapolateWhiteFromOneChannel(_observedColor.Green, 255, reaction.Green);
 
                                if (!extrapolated)                ExtrapolateWhiteFromOneChannel(_observedColor.Blue,  255, reaction.Blue);
 
                            }
 
                        }
 

	
 
                        if (!extrapolated)
 
                        {
 
                            State       = TestState.ClippedResult;
 
                            BadReaction = CalculateReaction();
 
                        }
 
                    }
 
                    
 
                    PlayerProfile? profile = ProfileManager.CurrentProfile;
 
                    profile?.SetPairClipStatus(Reagent1, Reagent2, Clipped);
 
                }
 
            }
 
            else
 
            {
 
                Debug.WriteLine("ERROR: Lab UI not found.");
 
                State = TestState.LabNotFound;
 
            }
 
        }
 

	
 
        private bool ExtrapolateWhiteFromOneChannel(int result, int clipBound, int reaction)
 
        {
 
            if (result == clipBound) return false;
 
            
 
            Clipped = ClipType.None;
 
            State = TestState.GoodResult;
 
            Reaction = new Reaction(reaction, reaction, reaction);
 

	
 
            return true;
 
        }
 

	
 

	
 
        public void CancelScan()
 
        {
 
            ReactionScannerService.Instance.CancelScan();
 
            State = TestState.Untested;
 
        }
 

	
 
        public void MarkInert()
 
        {
 
            Reaction = new Reaction(0, 0, 0);
 
            State = TestState.GoodResult;
 
            SaveReaction();
 
        }
 

	
 
        public void ClearReaction()
 
        {
 
            PlayerProfile? profile = ProfileManager.CurrentProfile;
 
            if (profile == null) return;
 
            profile.Reactions.Remove(Reagent1, Reagent2);
 
            profile.SetPairClipStatus(Reagent1, Reagent2, ClipType.None);
 
            if (State == TestState.Saved)
 
            {
 
                profile.Save();
 
            }
 

	
 
            BufferReagentFirst = null;
 
            BufferReagentLast = null;
 
            
 
            Reaction = null;
 
            BadReaction = null;
 
            Clipped = ClipType.None;
 
            State = TestState.Untested;
 
            ObservedColor = null; // Clear it out
 
            
 
            UpdateRecipe();
 
        }
 

	
 
        public void SaveReaction()
 
        {
 
            PlayerProfile? profile = ProfileManager.CurrentProfile;
 
            if (profile == null) return;
 
            profile.Reactions.Set(Reagent1, Reagent2, Reaction);
 
            profile.Save();
 
            State = TestState.Saved;
 
        }
 

	
 
        #endregion
 
        
 
        #region Internals
 

	
 
        private Reaction? CalculateReaction()
 
        {
 
            if (ProfileManager.CurrentProfile == null) return null;
 
            if (HypotheticalColor == null) return null;
 
            if (ObservedColor == null) return null;
 

	
 
            if (_useFirstBuffer)
 
            {
 
                if (BufferReagentFirst != null)
 
                {
 
                    return ReactionScannerService.Calculate3WayReaction(ProfileManager.CurrentProfile, HypotheticalColor,
 
                        ObservedColor, BufferReagentFirst, Reagent1, Reagent2, true);
 
                }
 
            }
 
            else
 
            {
 
                if (BufferReagentLast != null)
 
                {
 
                    return ReactionScannerService.Calculate3WayReaction(ProfileManager.CurrentProfile, HypotheticalColor,
 
                        ObservedColor, Reagent1, Reagent2, BufferReagentLast, false);
 
                }
 
            }
 
            
 
            return ReactionScannerService.CalculateReaction(HypotheticalColor, ObservedColor);
 
        }
 

	
 

	
 
        private void UpdateHypotheticalColor()
 
        {
 
            HypotheticalColor = null;
 
            HypotheticalColor = (IsAllCatalysts && !BufferIsSelected) ? null : _recipe.BaseColor;
 
        }
 

	
 
        private bool BufferIsSelected => UseFirstBuffer ? (BufferReagentFirst != null) : (BufferReagentLast != null);
 

	
 

	
 
        private void UpdateRecipe()
 
        {
 
            _recipe.Clear();
 
            if (_useFirstBuffer)
 
            {
 
                if (_bufferReagentFirst == null)
 
                {
 
                    _recipe.AddReagent(Reagent1.Name);
 
                    _recipe.AddReagent(Reagent2.Name);
 
                }
 
                else
0 comments (0 inline, 0 general)