Exemplo n.º 1
0
        private void DrawSatValPanel(Canvas canvas)
        {
            var rect = _satValRect;

            if (BorderWidthPx > 0)
            {
                _borderPaint.Color = _borderColor;
                canvas.DrawRect(_drawingRect.Left, _drawingRect.Top, rect.Right + BorderWidthPx, rect.Bottom + BorderWidthPx, _borderPaint);
            }

            if (_valShader == null)
            {
                _valShader = new LinearGradient(rect.Left, rect.Top, rect.Left, rect.Bottom,
                                                Color.Argb(255, 255, 255, 255), Color.Argb(255, 0, 0, 0), Shader.TileMode.Clamp);
            }

            var rgb = ColorUtils.ColorFromHSV(_hue / 360f, 1f, 1f);

            using (_satShader = new LinearGradient(rect.Left, rect.Top, rect.Right, rect.Top,
                                                   Color.Argb(255, 255, 255, 255), rgb, Shader.TileMode.Clamp))
            {
                var mShader = new ComposeShader(_valShader, _satShader, PorterDuff.Mode.Multiply);
                _satValPaint.SetShader(mShader);

                canvas.DrawRect(rect, _satValPaint);
            }
            var p = SatValToPoint(_sat, _val);

            _satValTrackerPaint.Color = Color.Argb(255, 0, 0, 0);
            canvas.DrawCircle(p.X, p.Y, _paletteCircleTrackerRadius - 1f * _density, _satValTrackerPaint);

            _satValTrackerPaint.Color = Color.Argb(255, 221, 221, 221);
            canvas.DrawCircle(p.X, p.Y, _paletteCircleTrackerRadius, _satValTrackerPaint);
        }
Exemplo n.º 2
0
        private Bitmap createColorWheelBitmap(int width, int height)
        {
            Bitmap bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);

            int colorCount     = 12;
            int colorAngleStep = 360 / 12;

            int[]   colors = new int[colorCount + 1];
            float[] hsv    = new float[] { 0f, 1f, 1f };
            for (int i = 0; i < colors.Length; i++)
            {
                hsv[0]    = (i * colorAngleStep + 180) % 360;
                colors[i] = Color.HSVToColor(hsv);
            }
            colors[colorCount] = colors[0];

            SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null);

            RadialGradient radialGradient = new RadialGradient(width / 2, height / 2, colorWheelRadius, new Color(0xFFFFFF), new Color(0x00FFFFFF), Shader.TileMode.Clamp);
            ComposeShader  composeShader  = new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SrcOver);

            colorWheelPaint.SetShader(composeShader);

            Canvas canvas = new Canvas(bitmap);

            canvas.DrawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint);

            return(bitmap);
        }
Exemplo n.º 3
0
        private ComposeShader CreateLinearGradient()
        {
            var           colorGradient        = new LinearGradient(0, 0, this.Width, 0, colors, null, Shader.TileMode.Clamp);
            var           illuminationGradient = new LinearGradient(0, 0, 0, this.Height, illumination, null, Shader.TileMode.Clamp);
            ComposeShader merged = new ComposeShader(colorGradient, illuminationGradient, PorterDuff.Mode.Darken);
            ComposeShader final  = new ComposeShader(merged, illuminationGradient, PorterDuff.Mode.Lighten);

            return(final);
        }
Exemplo n.º 4
0
        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);
            if (cShader == null)
            {
                cShader = CreateLinearGradient();
            }
            if (drawPaint == null)
            {
                CreatePaint();
            }

            if (gradientBitmap == null)
            {
                gradientBitmap = Bitmap.CreateBitmap(this.Width, this.Height, Bitmap.Config.Argb8888);
                Canvas canvasBitmap = new Canvas(gradientBitmap);
                canvasBitmap.DrawPaint(drawPaint);
                canvas.DrawBitmap(gradientBitmap, 0, 0, drawPaint);
            }
        }
Exemplo n.º 5
0
        private void DrawSatValPanel(Canvas canvas)
        {
            #if __ANDROID_11__
            if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.Honeycomb)
            {
                RootView.SetLayerType(LayerType.Software, null);
            }
            #endif

            var rect = _satValRect;

            if (BorderWidthPx > 0)
            {
                _borderPaint.Color = _borderColor;
                canvas.DrawRect(_drawingRect.Left, _drawingRect.Top, rect.Right + BorderWidthPx, rect.Bottom + BorderWidthPx, _borderPaint);
            }

            if (_valShader == null)
            {
                _valShader = new LinearGradient(rect.Left, rect.Top, rect.Left, rect.Bottom,
                                                Color.Argb(255, 255, 255, 255), Color.Argb(255, 0, 0, 0), Shader.TileMode.Clamp);
            }

            var rgb = ColorUtils.ColorFromHSV(_hue / 360f, 1f, 1f);

            using (_satShader = new LinearGradient(rect.Left, rect.Top, rect.Right, rect.Top,
                                                   Color.Argb(255, 255, 255, 255), rgb, Shader.TileMode.Clamp))
            {
                var mShader = new ComposeShader(_valShader, _satShader, PorterDuff.Mode.Multiply);
                _satValPaint.SetShader(mShader);

                canvas.DrawRect(rect, _satValPaint);
            }
            var p = SatValToPoint(_sat, _val);

            _satValTrackerPaint.Color = Color.Argb(255, 0, 0, 0);
            canvas.DrawCircle(p.X, p.Y, _paletteCircleTrackerRadius - 1f * _density, _satValTrackerPaint);

            _satValTrackerPaint.Color = Color.Argb(255, 221, 221, 221);
            canvas.DrawCircle(p.X, p.Y, _paletteCircleTrackerRadius, _satValTrackerPaint);
        }
Exemplo n.º 6
0
        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);

            if (_paint == null)
            {
                _paint = new Paint();
                _luar  = new LinearGradient(
                    0.0f,
                    0.0f,
                    0.0f,
                    MeasuredHeight,
                    unchecked ((int)0xffffffff).ToColor(),
                    unchecked ((int)0xff000000).ToColor(),
                    Shader.TileMode.Clamp
                    );
            }
            int    rgb   = Color.HSVToColor(_color);
            Shader dalam = new LinearGradient(
                0.0f,
                0.0f,
                MeasuredWidth,
                0.0f,
                unchecked ((int)0xffffffff).ToColor(),
                rgb.ToColor(),
                Shader.TileMode.Clamp
                );

            var shader = new ComposeShader(
                _luar,
                dalam,
                PorterDuff.Mode.Multiply
                );

            _paint.SetShader(shader);
            canvas.DrawRect(0.0f, 0.0f, MeasuredWidth, MeasuredHeight, _paint);
        }
Exemplo n.º 7
0
        private void DrawSatValPanel(Canvas canvas)
        {
            var rect = _satValRect;

            if (BORDER_WIDTH_PX > 0)
            {
                _borderPaint.Color = _borderColor;
                canvas.DrawRect(_drawingRect.Left, _drawingRect.Top,
                                rect.Right + BORDER_WIDTH_PX,
                                rect.Bottom + BORDER_WIDTH_PX, _borderPaint);
            }

            if (_valShader == null)
            {
                //Black gradient has either not been created or the view has been resized.
                _valShader = new LinearGradient(rect.Left, rect.Top, rect.Left, rect.Bottom, Color.White, Color.Black,
                                                Shader.TileMode.Clamp);
            }

            //If the hue has changed we need to recreate the cache.
            if (_satValBackgroundCache == null || _satValBackgroundCache.Value != _hue)
            {
                if (_satValBackgroundCache == null)
                {
                    _satValBackgroundCache = new BitmapCache();
                }

                //We create our bitmap in the cache if it doesn't exist.
                if (_satValBackgroundCache.Bitmap == null)
                {
                    _satValBackgroundCache.Bitmap = Bitmap
                                                    .CreateBitmap(rect.Width(), rect.Height(), Bitmap.Config.Argb8888);
                }

                //We create the canvas once so we can draw on our bitmap and the hold on to it.
                if (_satValBackgroundCache.Canvas == null)
                {
                    _satValBackgroundCache.Canvas = new Canvas(_satValBackgroundCache.Bitmap);
                }

                int rgb = Color.HSVToColor(new[] { _hue, 1f, 1f });

                _satShader = new LinearGradient(rect.Left, rect.Top, rect.Right, rect.Top, Color.White, new Color(rgb),
                                                Shader.TileMode.Clamp);

                var mShader = new ComposeShader(
                    _valShader, _satShader, PorterDuff.Mode.Multiply);
                _satValPaint.SetShader(mShader);

                // Finally we draw on our canvas, the result will be
                // stored in our bitmap which is already in the cache.
                // Since this is drawn on a canvas not rendered on
                // screen it will automatically not be using the
                // hardware acceleration. And this was the code that
                // wasn't supported by hardware acceleration which mean
                // there is no need to turn it of anymore. The rest of
                // the view will still be hw accelerated.
                _satValBackgroundCache.Canvas.DrawRect(0, 0,
                                                       _satValBackgroundCache.Bitmap.Width,
                                                       _satValBackgroundCache.Bitmap.Height,
                                                       _satValPaint);

                //We set the hue value in our cache to which hue it was drawn with,
                //then we know that if it hasn't changed we can reuse our cached bitmap.
                _satValBackgroundCache.Value = _hue;
            }

            // We draw our bitmap from the cached, if the hue has changed
            // then it was just recreated otherwise the old one will be used.
            canvas.DrawBitmap(_satValBackgroundCache.Bitmap, null, rect, null);

            var p = SatValToPoint(_sat, _val);

            _satValTrackerPaint.Color = new Color(0xff, 0x00, 0x00, 0x00);
            canvas.DrawCircle(p.X, p.Y, _circleTrackerRadiusPx - DrawingUtils.DpToPx(Context, 1), _satValTrackerPaint);

            _satValTrackerPaint.Color = new Color(0xff, 0xdd, 0xdd, 0xdd);
            canvas.DrawCircle(p.X, p.Y, _circleTrackerRadiusPx, _satValTrackerPaint);
        }
Exemplo n.º 8
0
        private void drawSatValPanel(Canvas canvas)
        {
            /*
             * Draw time for this code without using bitmap cache and hardware acceleration was around 20ms.
             * Now with the bitmap cache and the ability to use hardware acceleration we are down at 1ms as long as the hue isn't changed.
             * If the hue is changed we the sat/val rectangle will be rendered in software and it takes around 10ms.
             * But since the rest of the view will be rendered in hardware the performance gain is big!
             */

            RectF rect = mSatValRect;

            if (BORDER_WIDTH_PX > 0)
            {
                //TODO : Cross check the conversion
                byte[] byteArr = BitConverter.GetBytes(mBorderColor);
                mBorderPaint.Color = Color.Argb(byteArr[0], byteArr[1], byteArr[2], byteArr[3]);
                //mBorderPaint.Color = mBorderColor;

                canvas.DrawRect(mDrawingRect.Left, mDrawingRect.Top, rect.Right + BORDER_WIDTH_PX, rect.Bottom + BORDER_WIDTH_PX, mBorderPaint);
            }

            if (mValShader == null)
            {
                //TODO: Set as per native lib
                //Black gradient has either not been created or the view has been resized.
                mValShader = new LinearGradient(rect.Left, rect.Top, rect.Left, rect.Bottom,
                                                Color.White, Color.Black, Android.Graphics.Shader.TileMode.Clamp);
            }


            //If the hue has changed we need to recreate the cache.
            if (mSatValBackgroundCache == null || mSatValBackgroundCache.value != mHue)
            {
                if (mSatValBackgroundCache == null)
                {
                    mSatValBackgroundCache = new BitmapCache();
                }

                //We create our bitmap in the cache if it doesn't exist.
                if (mSatValBackgroundCache.bitmap == null)
                {
                    mSatValBackgroundCache.bitmap = Bitmap.CreateBitmap((int)rect.Width(), (int)rect.Height(), Bitmap.Config.Argb8888);
                }

                //We create the canvas once so we can draw on our bitmap and the hold on to it.
                if (mSatValBackgroundCache.canvas == null)
                {
                    mSatValBackgroundCache.canvas = new Canvas(mSatValBackgroundCache.bitmap);
                }

                Color rgb = Color.HSVToColor(new float[] { mHue, 1f, 1f });

                //TODO: set as per native client
                mSatShader = new LinearGradient(rect.Left, rect.Top, rect.Right, rect.Top,
                                                Color.White, rgb, Android.Graphics.Shader.TileMode.Clamp);

                ComposeShader mShader = new ComposeShader(mValShader, mSatShader, PorterDuff.Mode.Multiply);
                mSatValPaint.SetShader(mShader);

                //Finally we draw on our canvas, the result will be stored in our bitmap which is already in the cache.
                //Since this is drawn on a canvas not rendered on screen it will automatically not be using the hardware acceleration.
                //And this was the code that wasn't supported by hardware acceleration which mean there is no need to turn it of anymore.
                //The rest of the view will still be hardware accelerated!!
                mSatValBackgroundCache.canvas.DrawRect(0, 0, mSatValBackgroundCache.bitmap.Width, mSatValBackgroundCache.bitmap.Height, mSatValPaint);

                //We set the hue value in our cache to which hue it was drawn with,
                //then we know that if it hasn't changed we can reuse our cached bitmap.
                mSatValBackgroundCache.value = mHue;
            }

            //We draw our bitmap from the cached, if the hue has changed
            //then it was just recreated otherwise the old one will be used.
            canvas.DrawBitmap(mSatValBackgroundCache.bitmap, null, rect, null);


            Point p = satValToPoint(mSat, mVal);

            //TODO: set as per native lib
            mSatValTrackerPaint.Color = Color.Black;
            canvas.DrawCircle(p.X, p.Y, PALETTE_CIRCLE_TRACKER_RADIUS - 1f * mDensity, mSatValTrackerPaint);

            //TODO: set as per native lib
            mSatValTrackerPaint.Color = Color.Blue;
            canvas.DrawCircle(p.X, p.Y, PALETTE_CIRCLE_TRACKER_RADIUS, mSatValTrackerPaint);
        }
Exemplo n.º 9
0
        private void DrawSatValPanel(Canvas canvas)
        {
            /*
             * Draw time for this code without using bitmap cache and hardware acceleration was around 20ms.
             * Now with the bitmap cache and the ability to use hardware acceleration we are down at 1ms as long as the hue isn't changed.
             * If the hue is changed we the sat/val rectangle will be rendered in software and it takes around 10ms.
             * But since the rest of the view will be rendered in hardware the performance gain is big!
             */

            RectF rect = _mSatValRect;

            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            if (BORDER_WIDTH_PX > 0)
            {
                _mBorderPaint.Color = new Color((int)_mBorderColor);
                canvas.DrawRect(_mDrawingRect.Left, _mDrawingRect.Top, rect.Right + BORDER_WIDTH_PX, rect.Bottom + BORDER_WIDTH_PX, _mBorderPaint);
            }

            if (_mValShader == null)
            {
                //Black gradient has either not been created or the view has been resized.
                uint startColor = 0xFFFFFFFF;
                uint endColor   = 0xFF000000;
                _mValShader = new LinearGradient(rect.Left, rect.Top, rect.Left, rect.Bottom,
                                                 new Color((int)startColor), new Color((int)endColor), Shader.TileMode.Clamp);
            }


            //If the hue has changed we need to recreate the cache.
            // ReSharper disable once CompareOfFloatsByEqualityOperator
            if (_mSatValBackgroundCache == null || _mSatValBackgroundCache.Value != _mHue)
            {
                if (_mSatValBackgroundCache == null)
                {
                    _mSatValBackgroundCache = new BitmapCache();
                }

                //We create our bitmap in the cache if it doesn't exist.
                if (_mSatValBackgroundCache.Bitmap == null)
                {
                    _mSatValBackgroundCache.Bitmap = Bitmap.CreateBitmap((int)rect.Width(), (int)rect.Height(), Bitmap.Config.Argb8888);
                }

                //We create the canvas once so we can draw on our bitmap and the hold on to it.
                if (_mSatValBackgroundCache.Canvas == null)
                {
                    _mSatValBackgroundCache.Canvas = new Canvas(_mSatValBackgroundCache.Bitmap);
                }

                int  rgb        = Color.HSVToColor(new[] { _mHue, 1f, 1f });
                uint startColor = 0xFFFFFFFF;
                _mSatShader = new LinearGradient(rect.Left, rect.Top, rect.Right, rect.Top,
                                                 new Color((int)startColor), new Color(rgb), Shader.TileMode.Clamp);

                ComposeShader mShader = new ComposeShader(_mValShader, _mSatShader, PorterDuff.Mode.Multiply);
                _mSatValPaint.SetShader(mShader);

                //ly we draw on our canvas, the result will be stored in our bitmap which is already in the cache.
                //Since this is drawn on a canvas not rendered on screen it will automatically not be using the hardware acceleration.
                //And this was the code that wasn't supported by hardware acceleration which mean there is no need to turn it of anymore.
                //The rest of the view will still be hardware accelerated!!
                _mSatValBackgroundCache.Canvas.DrawRect(0, 0, _mSatValBackgroundCache.Bitmap.Width, _mSatValBackgroundCache.Bitmap.Height, _mSatValPaint);

                //We set the hue value in our cache to which hue it was drawn with,
                //then we know that if it hasn't changed we can reuse our cached bitmap.
                _mSatValBackgroundCache.Value = _mHue;
            }

            //We draw our bitmap from the cached, if the hue has changed
            //then it was just recreated otherwise the old one will be used.
            canvas.DrawBitmap(_mSatValBackgroundCache.Bitmap, null, rect, null);


            Point p = SatValToPoint(_mSat, _mVal);

            _mSatValTrackerPaint.Color = Color.Black;
            canvas.DrawCircle(p.X, p.Y, _paletteCircleTrackerRadius - 1f * _mDensity, _mSatValTrackerPaint);

            _mSatValTrackerPaint.Color = Color.LightGray;
            canvas.DrawCircle(p.X, p.Y, _paletteCircleTrackerRadius, _mSatValTrackerPaint);
        }