Click here to Skip to main content
15,896,207 members
Articles / Multimedia / GDI+

Photoshop-Like Gradient Editor

Rate me:
Please Sign up or sign in to vote.
4.31/5 (15 votes)
16 Feb 2006Ms-RL3 min read 57.2K   1.5K   41  
A Photoshop-like gradient builder control for paint applications.
#region - Terms of Usage -
/*
 * Copyright 2006 Sameera Perera
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
#endregion   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Inferno.Utilities
{
    public partial class GradientTypeEditorUI : UserControl
    {
        private Marker marker;

        public event EventHandler GradientChanged;

        [Browsable(false), DefaultValue(null)]
        public Gradient Gradient
        {
            get { return this.gradientBuilder.Gradient; }
            set { this.gradientBuilder.Gradient = value; }
        }

        // <summary>
        /// Gets or sets whether the GradientChanged event is fired when a marker
        /// is dragged.
        /// </summary>
        [DefaultValue(false)]
        public bool SilentMarkers
        {
            get { return this.gradientBuilder.SilentMarkers; }
            set { this.gradientBuilder.SilentMarkers = value; }
        }
        
        public GradientTypeEditorUI()
        {
            InitializeComponent();

            colorEditor.EditedType  = typeof( Color );
            colorEditor.Value       = Color.White;

            Font editorFont         = new Font( this.Font.FontFamily, 7.0f );
            colorEditor.Font        = editorFont;
        }

        private void gradientBuilder_MarkerSelected( object sender, EventArgs e )
        {
            Marker tempMarker   = sender as Marker;
            marker              = null; // Freeze all other event handlers
            if ( tempMarker != null )
            {
                colorEditor.Value   = tempMarker.Color;
                opacityBox.Value    = Convert.ToDecimal( tempMarker.Color.A * ( 100.0f/255 ) );
                positionBox.Enabled = tempMarker.IsMovable;
                positionBox.Value   = (int)(tempMarker.Position * 100.0f);
                grpEditor.Enabled   = true;
            }
            else
                grpEditor.Enabled = false;
            marker  = tempMarker;
        }

        private void colorMixerFieldValueChanged( object sender, EventArgs e )
        {
            if ( marker != null )
                UpdateMarkerColor();
        }

        private void UpdateMarkerColor()
        {
            Color color     = (Color)colorEditor.Value;
            int a           = Convert.ToInt32( (float)opacityBox.Value * ( 255.0f / 100 ) );
            marker.Color    = Color.FromArgb( a, color );
            if ( marker.IsMovable )
                marker.Position = (float)positionBox.Value / 100.0f;
        }

        private void gradientBuilder_GradientChanged( object sender, EventArgs e )
        {
            if ( this.GradientChanged != null )
                GradientChanged( this, EventArgs.Empty );
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under Microsoft Reciprocal License


Written By
Architect
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions