private Color?manipulateRgb(Color color, string[] tokens, string text, TextBoxKeyPressingEventArgs args)
        {
            if (args.PressedKey != Key.Down && args.PressedKey != Key.Up)
            {
                return(null);
            }

            int caret      = args.IntendedState.CaretPosition;
            int firstComma = text.IndexOf(',');

            if (caret <= firstComma)
            {
                return(Color.FromRgba(manipulateColorUnit(color.R, args), color.G, color.B, color.A));
            }

            int secondComma = nthIndexOfComma(text, 0, 2);

            if (caret <= secondComma)
            {
                return(Color.FromRgba(color.R, manipulateColorUnit(color.G, args), color.B, color.A));
            }

            if (tokens.Length == 4)
            {
                int thirdComma = nthIndexOfComma(text, 0, 3);
                if (caret > thirdComma)
                {
                    return(Color.FromRgba(color.R, color.G, color.B, manipulateColorUnit(color.A, args)));
                }
            }
            return(Color.FromRgba(color.R, color.G, manipulateColorUnit(color.B, args), color.A));
        }
Exemple #2
0
        private void onKeyDown(object sender, KeyboardEventArgs args)
        {
            if (args.Key == Key.ShiftLeft)
            {
                _leftShiftOn = true; return;
            }
            if (args.Key == Key.ShiftRight)
            {
                _rightShiftOn = true; return;
            }

            if (!IsFocused)
            {
                return;
            }
            TextBoxKeyPressingEventArgs pressingArgs = new TextBoxKeyPressingEventArgs(args.Key);

            OnPressingKey.Invoke(this, pressingArgs);
            if (pressingArgs.ShouldCancel)
            {
                return;
            }

            processKey(args.Key);
        }
        private bool parseRgbColor(string text, TextBoxKeyPressingEventArgs args)
        {
            var tokens = text.Split(',');

            if (tokens.Length != 3 && tokens.Length != 4)
            {
                return(false);
            }
            if (!byte.TryParse(tokens[0], out byte r))
            {
                return(false);
            }
            if (!byte.TryParse(tokens[1], out byte g))
            {
                return(false);
            }
            if (!byte.TryParse(tokens[2], out byte b))
            {
                return(false);
            }
            byte a = 255;

            if (tokens.Length == 4)
            {
                if (!byte.TryParse(tokens[3], out a))
                {
                    return(false);
                }
            }

            var color    = Color.FromRgba(r, g, b, a);
            var newColor = manipulateRgb(color, tokens, text, args);

            if (newColor != null)
            {
                color = newColor.Value;
                if (tokens.Length == 4)
                {
                    args.IntendedState.Text = $"{color.R},{color.G},{color.B},{color.A}";
                }
                else
                {
                    args.IntendedState.Text = $"{color.R},{color.G},{color.B}";
                }
            }
            _property.Prop.SetValue(_property.Object, color);
            return(true);
        }
 private byte manipulateColorUnit(byte unit, TextBoxKeyPressingEventArgs args)
 {
     if (unit <= 0 && args.PressedKey == Key.Down)
     {
         return(unit);
     }
     if (unit >= 255 && args.PressedKey == Key.Up)
     {
         return(unit);
     }
     if (args.PressedKey == Key.Down)
     {
         return((byte)(unit - 1));
     }
     return((byte)(unit + 1));
 }
Exemple #5
0
        private void onTextBoxKeyPressed(TextBoxKeyPressingEventArgs args)
        {
            if (args.PressedKey != Key.Enter)
            {
                return;
            }
            args.ShouldCancel = true;
            string path = _fileTextBox.Text;

            if (_device.FileSystem.DirectoryExists(path))
            {
                fillAllFiles(path);
            }
            else if (_device.FileSystem.FileExists(path))
            {
                onFileSelected(path);
            }
        }
        private void onTextboxPressingKey(TextBoxKeyPressingEventArgs args)
        {
            string text = args.IntendedState.Text;

            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            //except for selecting pre-set colors from the dropdown, we'll allow multiple formats in the
            //textbox for specifying the color:
            //1. "{R},{G},{B}" or "{R},{G},{B},{A}": those would be from 0-255
            //2. "#{HEXA}" for a hexa color number

            if (parseHexaColor(text))
            {
                return;
            }
            parseRgbColor(text, args);
        }