⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colorslider.cs

📁 Fireball.CodeEditor is an source code editor control derived from the best compona SyntaxBox Control
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Fireball.Drawing.Drawing2D;

namespace Fireball.Drawing.Design
{
    public class ColorSlider : Control
    {
        public enum ColorSliderOrientation
        {
            Vertical=0,Horizontal=1
        }

        private ColorHLS _baseColor;
        private ColorHLS _currentColor;
        private ColorHLS _previusColor;
        private ColorSliderMode _colorMode;
        private Rectangle _cursorRect;
        private bool _onDrag;
        private ColorSliderOrientation _orientation;

        private float _saturation;
        private float _luminance;
        private float _hue;

        private PixelBuffer _pixBuffer;

        internal event ColorChangedDelegate ColorChanged;

        public ColorSlider()
            : this(ColorSliderMode.Hue,ColorSliderOrientation.Vertical)
        {
        }
        public ColorSlider(ColorSliderMode colorMode,ColorSliderOrientation orientation)
        {
            _colorMode = colorMode;
            _orientation = orientation;

            _baseColor = new ColorHLS(255, 255, 0, 0);
            _currentColor = _baseColor.Clone();
            _previusColor = _baseColor.Clone();

            this.SetStyle(
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.FixedHeight |
                ControlStyles.FixedWidth |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.UserPaint,
                true);

            if (_orientation == ColorSliderOrientation.Vertical)
            {
                this.Size = new System.Drawing.Size(20, 256);
                _cursorRect = new Rectangle(0, 256 - 4, 20, 7);
            }
            else
            {
                this.Size = new System.Drawing.Size(256, 20);
                _cursorRect = new Rectangle(- 3,0 , 7, 20);
            }

            
            _onDrag = false;

            //_pixBuffer = new PixelBuffer(this.Width, this.Height);
            //FillBuffer();

            this.Invalidate();
        }

        public ColorSliderMode ColorMode
        {
            get
            {
                return _colorMode;
            }
            set
            {
                _colorMode = value;
                UpdateControl();
                this.Invalidate();
            }
        }
        public ColorHLS BaseColor
        {
            get
            {
                return _baseColor;
            }
            set
            {
                _baseColor = value;
                ChangeBaseColor(_baseColor);
            }
        }
        /*public ColorHLS CurrentColor
        {
            get 
            { 
                return _currentColor; 
            }
            set 
            { 
                _currentColor = value; 
            }
        }*/
        /*public float Saturation
        {
            get
            {
                return _saturation;
            }
        }
        public float Brightness
        {
            get
            {
                return _brightness;
            }
        }
        public float Hue
        {
            get
            {
                return _hue; ;
            }
        }*/
        public float Value
        {
            get
            {
                switch (_colorMode)
                {
                    default:
                    case ColorSliderMode.Hue:
                        return _hue;
                    case ColorSliderMode.Saturation:
                        return _saturation;
                    case ColorSliderMode.Luminance:
                        return _luminance;
                }
            }
            set
            {
                SetValue(value);
            }
        }
        public ColorSliderOrientation Orientation
        {
            get
            {
                return _orientation;
            }
            set
            {
                _orientation = value;
                this.OnSizeChanged(new EventArgs());
                
            }
        }

        private void SetValue(float value)
        {

            if (_orientation == ColorSliderOrientation.Vertical)
            {
                float y = -3;
                switch (_colorMode)
                {
                    default:
                    case ColorSliderMode.Hue:
                        value %= 360;
                        y = 255 - (((value / 359) * 255) + 3);
                        _hue = value;
                        break;
                    case ColorSliderMode.Saturation:
                        if (value < 0 || value > 1)
                        {
                            throw new ArgumentOutOfRangeException("value");
                        }
                        y = (((1 - value) * 255)) - 3;
                        _saturation = value;
                        break;
                    case ColorSliderMode.Luminance:
                        if (value < 0 || value > 1)
                        {
                            throw new ArgumentOutOfRangeException("value");
                        }
                        y = ((1 - value) * 255) - 3;
                        _luminance = value;
                        break;
                }
                _cursorRect.Y = (int)y;
                _currentColor = GetColorAt(1, _cursorRect.Y + 3);
            }
            else
            {
                float x = -3;
                switch (_colorMode)
                {
                    default:
                    case ColorSliderMode.Hue:
                        value %= 360;
                        x = (((value / 359) * 255) - 3);
                        _hue = value;
                        break;
                    case ColorSliderMode.Saturation:
                        if (value < 0 || value > 1)
                        {
                            throw new ArgumentOutOfRangeException("value");
                        }
                        x = (value * 255) - 3;
                        _saturation = value;
                        break;
                    case ColorSliderMode.Luminance:
                        if (value < 0 || value > 1)
                        {
                            throw new ArgumentOutOfRangeException("value");
                        }
                        x = (value * 255) - 3;
                        _luminance = value;
                        break;
                }
                _cursorRect.X = (int)x;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -