Линейный градиент

Описание: Пограммы для скриптеров
Модератор: SJplayer

Леонид M
Автор темы, Рядовой
Рядовой
Аватара
Леонид M
Автор темы, Рядовой
Рядовой
Сообщения: 11
Зарегистрирован: 25 мая 2014
С нами: 9 лет 10 месяцев

#1 Леонид » 11 апреля 2015, 18:21

Линейный градиент (переход от одного цвета к другому в N шагов)

Программа, которая генерирует цвета при переходе от одного цвета к другому в N шагов. Иначе говоря, линейный градиент.

Изображение


Требуется: Net FrameWork 4.5 - Поддерживаемые операционные системы: Windows 7 Service Pack 1; Windows Server 2008 R2 SP1; Windows Server 2008 Service Pack 2; Windows Vista Service Pack 2.

Скачать

Скачать для XP

Код: Выделить всё

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


namespace ColorChanger
{
    public partial class Form1 : Form
    
{
        Panel[] ColorView;
        int OldSteps = 0;

        static void HexToRGB(Color HEX_COLOR, ref int R, ref int G, ref int B)
        {
             R = HEX_COLOR.R;
             G = HEX_COLOR.G;
             B = HEX_COLOR.B;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void ChangeColor(object sender, EventArgs e)
        {
            MaskedTextBox A = (sender as MaskedTextBox); 
            if 
(A.Text.Length > 0)
            {
                int R = 0, G = 0, B = 0;
                object ChangePanel = panel1;
                try
                
{
                    HexToRGB(ColorTranslator.FromHtml('#' + A.Text), ref R, ref G, ref B);
                    if (sender == maskedTextBox2) ChangePanel = panel2;
                    (ChangePanel as Panel).BackColor = Color.FromArgb(255, R, G, B);
                }
                catch
                
{
                    A.Text = A.Text.Substring(0, A.Text.Length - 1);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
           
            if 
(maskedTextBox1.Text.Length > 0 && maskedTextBox2.Text.Length > 0 && maskedTextBox3.Text.Length > 0)
            {
                int Steps = Convert.ToInt32(maskedTextBox3.Text);
                if (Steps >= 1 && Steps <= 100)
                {
                    if (OldSteps > 0)
                    {
                        for (int i = 0; i != OldSteps; i++) ColorView[i].Dispose();
                    }

                    ColorView = new Panel[Steps];
                    int R = 0, G = 0, B = 0, R1 = 0, G1 = 0, B1 = 0;
                    HexToRGB(ColorTranslator.FromHtml('#' + maskedTextBox1.Text), ref R, ref G, ref B);
                    HexToRGB(ColorTranslator.FromHtml('#' + maskedTextBox2.Text), ref R1, ref G1, ref B1);

                    int Rs = 0, Gs = 0, Bs = 0;
                    Rs = (R1 - R) / Steps;
                    Gs = (G1 - G) / Steps;
                    Bs = (B1 - B) / Steps;

                    int SizeBlock = 336 / Steps;
                    OldSteps = Steps;

                    int Difference = Steps - (336 - SizeBlock * Steps);

                    for (int i = 0, ColorIndex = 43; i != Steps; i++, ColorIndex += SizeBlock)
                    {
                        if (== Difference) SizeBlock++;

                        R += Rs;
                        G += Gs;
                        B += Bs;
                        String S = ColorTranslator.ToHtml(Color.FromArgb(255, R, G, B));
                        textBox1.Text += (S.Substring(1, S.Length - 1) + Environment.NewLine);

                        ColorView[i] = new Panel();
                        ColorView[i].Location = new Point(ColorIndex, 12);
                        ColorView[i].Size = new Size(SizeBlock, 80);
                        ColorView[i].BackColor = Color.FromArgb(255, R, G, B);
                        ColorView[i].Visible = true;
                        this.Controls.Add(ColorView[i]);
                    }
                }
                else textBox1.Text = "Число шагов от 1 до 100.";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(textBox1.Text, true);
        }
    }
}


Вернуться в «Пограммы для скриптеров»

Кто сейчас на форуме (по активности за 5 минут)

Сейчас этот раздел просматривают: 7 гостей