Ejemplo n.º 1
0
        Color _nameToColor(string name)
        {
            D.assert(name.Length > 1);
            int   hash = name.GetHashCode() & 0xffff;
            float hue  = (360.0f * hash / (1 << 15)) % 360.0f;

            return(HSVColor.fromAHSV(1.0f, hue, 0.4f, 0.90f).toColor());
        }
Ejemplo n.º 2
0
        public override void paint(Canvas canvas, Size size)
        {
            Rect rect      = Offset.zero & size;
            var  gradientV = new LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: new List <Color> {
                Colors.white, Colors.black
            }
                );
            var gradientH = new LinearGradient(
                colors: new List <Color> {
                Colors.white,
                HSVColor.fromAHSV(1f, hsvColor.hue, 1f, 1f).toColor()
            }
                );

            var paint = new Paint()
            {
                strokeWidth = 1.5f, style = PaintingStyle.stroke
            };

            if (pointerColor != null)
            {
                paint.color = pointerColor;
            }
            else if (Utils.useWhiteForeground(hsvColor.toColor()))
            {
                paint.color = Colors.white;
            }
            else
            {
                paint.color = Colors.black;
            }


            canvas.drawRect(rect, new Paint()
            {
                shader = gradientV.createShader(rect)
            });
            canvas.drawRect(rect, new Paint()
            {
                blendMode = BlendMode.multiply, shader = gradientH.createShader(rect)
            });
            canvas.drawCircle(
                new Offset(size.width * hsvColor.saturation, size.height * (1 - hsvColor.value)),
                size.height * 0.04f,
                paint
                );
        }
Ejemplo n.º 3
0
        public static HSVColor fromColor(Color color)
        {
            double red   = (double)color.red / 0xFF;
            double green = (double)color.green / 0xFF;
            double blue  = (double)color.blue / 0xFF;

            double max   = Math.Max(red, Math.Max(green, blue));
            double min   = Math.Min(red, Math.Min(green, blue));
            double delta = max - min;

            double alpha      = (double)color.alpha / 0xFF;
            double hue        = _getHue(red, green, blue, max, delta);
            double saturation = max == 0.0 ? 0.0 : delta / max;

            //UnityEngine.Debug.Log($"{alpha}/{hue}/{saturation}/{max}");

            return(HSVColor.fromAHSV((float)alpha, (float)hue, (float)saturation, (float)max));
        }
Ejemplo n.º 4
0
        /// reference: https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV
        public static HSVColor hslToHsv(HSLColor color)
        {
            float s = 0f;
            float v = 0f;

            v = color.lightness +
                color.saturation *
                (color.lightness < 0.5 ? color.lightness : 1 - color.lightness);
            if (v != 0)
            {
                s = 2 - 2 * color.lightness / v;
            }

            return(HSVColor.fromAHSV(
                       color.alpha,
                       color.hue,
                       s.clamp(0f, 1f),
                       v.clamp(0f, 1f)
                       ));
        }
Ejemplo n.º 5
0
        public override Widget build(BuildContext context)
        {
            float thumbOffset = 15f;
            Color _thumbColor = Colors.black;

            return(new LayoutBuilder(builder: (BuildContext _, BoxConstraints box) =>
            {
                switch (trackType)
                {
                case TrackType.hue:
                    thumbOffset += (box.maxWidth - 30f) * hsvColor.hue / 360f;
                    _thumbColor = HSVColor.fromAHSV(1f, hsvColor.hue, 1f, 1f).toColor();
                    break;

                case TrackType.saturation:
                    thumbOffset += (box.maxWidth - 30f) * hsvColor.saturation;
                    _thumbColor =
                        HSVColor.fromAHSV(1f, hsvColor.hue, hsvColor.saturation, 1f)
                        .toColor();
                    break;

                case TrackType.saturationForHSL:
                    thumbOffset += (box.maxWidth - 30f) * Utils.hsvToHsl(hsvColor).saturation;
                    _thumbColor = HSLColor.fromAHSL(
                        1f, hsvColor.hue, Utils.hsvToHsl(hsvColor).saturation, 0.5f)
                                  .toColor();
                    break;

                case TrackType.value:
                    thumbOffset += (box.maxWidth - 30f) * hsvColor.value;
                    _thumbColor = HSVColor.fromAHSV(1f, hsvColor.hue, 1f, hsvColor.value)
                                  .toColor();
                    break;

                case TrackType.lightness:
                    thumbOffset += (box.maxWidth - 30f) * Utils.hsvToHsl(hsvColor).lightness;
                    _thumbColor = HSLColor.fromAHSL(
                        1f, hsvColor.hue, 1f, Utils.hsvToHsl(hsvColor).lightness)
                                  .toColor();
                    break;

                case TrackType.red:
                    thumbOffset += (box.maxWidth - 30f) * hsvColor.toColor().red / 0xff;
                    _thumbColor = hsvColor.toColor().withOpacity(1f);
                    break;

                case TrackType.green:
                    thumbOffset +=
                        (box.maxWidth - 30f) * hsvColor.toColor().green / 0xff;
                    _thumbColor = hsvColor.toColor().withOpacity(1f);
                    break;

                case TrackType.blue:
                    thumbOffset += (box.maxWidth - 30f) * hsvColor.toColor().blue / 0xff;
                    _thumbColor = hsvColor.toColor().withOpacity(1f);
                    break;

                case TrackType.alpha:
                    thumbOffset += (box.maxWidth - 30f) * hsvColor.toColor().opacity;
                    _thumbColor = Colors.black.withOpacity(hsvColor.alpha);
                    break;
                }//switch

                return new CustomMultiChildLayout(
                    layoutDelegate: new _SliderLayout(),
                    children: new List <Widget> {
                    new LayoutId(
                        id: _SliderLayout.track,
                        child: new ClipRRect(
                            borderRadius: BorderRadius.all(Radius.circular(3f)),
                            child: new CustomPaint(
                                painter: new TrackPainter(this.trackType, this.hsvColor)
                                ) //CustomPaint
                            )
                        ),        //LayoutId
                    new LayoutId(
                        id: _SliderLayout.thumb,
                        child: Transform.translate(
                            offset: new Offset(thumbOffset, 0f),
                            child: new CustomPaint(
                                painter: new ThumbPainter(
                                    thumbColor: !displayThumbColor ? null : _thumbColor,
                                    fullThumbColor: fullThumbColor
                                    ) //ThumbPainter
                                )     //CustomPaint
                            )         //Transform
                        ),            //LayoutId
                    new LayoutId(
                        id: _SliderLayout.gestureContainer,
                        child: new LayoutBuilder(
                            builder: (BuildContext __, BoxConstraints _box) => {
                        RenderBox getBox = context.findRenderObject() as RenderBox;
                        return new GestureDetector(
                            child: new Container(color: Colors.transparent),
                            onPanDown: (DragDownDetails details) => { slideEvent(getBox, box, details.globalPosition); },
                            onPanUpdate: (DragUpdateDetails details ) => { slideEvent(getBox, box, details.globalPosition); }
                            );
                    }
                            ) //LayoutBuilder
                        )     //LayoutId
                }             //list Widget
                    );        //CustomMultiChildLayout
            }));              //LayoutBuilder
        }
Ejemplo n.º 6
0
        public override void paint(Canvas canvas, Size size)
        {
            Rect rect = Offset.zero & size;

            if (trackType == TrackType.alpha)
            {
                Size  chessSize   = new Size(size.height / 2, size.height / 2);
                Paint chessPaintB = new Paint()
                {
                    color = new Color(0xffcccccc)
                };
                Paint chessPaintW = new Paint()
                {
                    color = Colors.white
                };

                int height = (size.height / chessSize.height).round();
                int width  = (size.width / chessSize.width).round();
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        canvas.drawRect(
                            new Offset(chessSize.width * x, chessSize.width * y) & chessSize,
                            (x + y) % 2 != 0 ? chessPaintW : chessPaintB
                            );
                    }
                }
            }

            List <Color>   colors;
            LinearGradient gradient;

            switch (trackType)
            {
            case TrackType.hue:
                colors = new List <Color>
                {
                    HSVColor.fromAHSV(1f, 0f, 1f, 1f).toColor(),
                    HSVColor.fromAHSV(1f, 60f, 1f, 1f).toColor(),
                    HSVColor.fromAHSV(1f, 120f, 1f, 1f).toColor(),
                    HSVColor.fromAHSV(1f, 180f, 1f, 1f).toColor(),
                    HSVColor.fromAHSV(1f, 240f, 1f, 1f).toColor(),
                    HSVColor.fromAHSV(1f, 240f, 1f, 1f).toColor(),
                    HSVColor.fromAHSV(1f, 300f, 1f, 1f).toColor(),
                    HSVColor.fromAHSV(1f, 360f, 1f, 1f).toColor()
                };
                gradient = new LinearGradient(colors: colors);
                canvas.drawRect(rect, new Paint()
                {
                    shader = gradient.createShader(rect)
                });
                break;

            case TrackType.saturation:
                colors = new List <Color> {
                    HSVColor.fromAHSV(1f, hsvColor.hue, 0f, 1f).toColor(),
                    HSVColor.fromAHSV(1f, hsvColor.hue, 1f, 1f).toColor()
                };
                gradient = new LinearGradient(colors: colors);
                canvas.drawRect(rect, new Paint()
                {
                    shader = gradient.createShader(rect)
                });
                break;

            case TrackType.saturationForHSL:
                colors = new List <Color> {
                    HSVColor.fromAHSV(1f, hsvColor.hue, 0f, .5f).toColor(),
                    HSVColor.fromAHSV(1f, hsvColor.hue, 1f, .5f).toColor()
                };
                gradient = new LinearGradient(colors: colors);
                canvas.drawRect(rect, new Paint()
                {
                    shader = gradient.createShader(rect)
                });
                break;

            case TrackType.value:
                colors = new List <Color> {
                    HSVColor.fromAHSV(1f, hsvColor.hue, 1f, 0f).toColor(),
                    HSVColor.fromAHSV(1f, hsvColor.hue, 1f, 1f).toColor()
                };
                gradient = new LinearGradient(colors: colors);
                canvas.drawRect(rect, new Paint()
                {
                    shader = gradient.createShader(rect)
                });
                break;

            case TrackType.lightness:
                colors = new List <Color> {
                    HSVColor.fromAHSV(1f, hsvColor.hue, 1f, 0f).toColor(),
                    HSVColor.fromAHSV(1f, hsvColor.hue, 1f, 0.5f).toColor(),
                    HSVColor.fromAHSV(1f, hsvColor.hue, 1f, 1f).toColor()
                };
                gradient = new LinearGradient(colors: colors);
                canvas.drawRect(rect, new Paint()
                {
                    shader = gradient.createShader(rect)
                });
                break;

            case TrackType.red:
                colors = new List <Color> {
                    hsvColor.toColor().withRed(0).withOpacity(1f),
                    hsvColor.toColor().withRed(255).withOpacity(1f)
                };
                gradient = new LinearGradient(colors: colors);
                canvas.drawRect(rect, new Paint()
                {
                    shader = gradient.createShader(rect)
                });
                break;

            case TrackType.green:
                colors = new List <Color> {
                    hsvColor.toColor().withGreen(0).withOpacity(1f),
                    hsvColor.toColor().withGreen(255).withOpacity(1f)
                };
                gradient = new LinearGradient(colors: colors);
                canvas.drawRect(rect, new Paint()
                {
                    shader = gradient.createShader(rect)
                });
                break;

            case TrackType.blue:
                colors = new List <Color> {
                    hsvColor.toColor().withBlue(0).withOpacity(1f),
                    hsvColor.toColor().withBlue(255).withOpacity(1f)
                };
                gradient = new LinearGradient(colors: colors);
                canvas.drawRect(rect, new Paint()
                {
                    shader = gradient.createShader(rect)
                });
                break;

            case TrackType.alpha:
                colors = new List <Color> {
                    Colors.black.withOpacity(0f),
                    Colors.black.withOpacity(1f)
                };
                gradient = new LinearGradient(colors: colors);
                canvas.drawRect(rect, new Paint()
                {
                    shader = gradient.createShader(rect)
                });
                break;
            }
        }