2010-12-24 18:51:59Chris C.S Huang

[C#] RGB to Lab色彩轉換工具

 


程式碼如下:

//        sRGB system: D50
//        RGB2Lab : sRGB system, D50
//        作者: Chris C.S Huang
//        程式語言: VC# 2008 Expression Edition
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using CsharpCreateDLL;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int R; int G; int B; int A;
        string HexColor;
        ColorSpace colorspace = new ColorSpace();
        public Form1()
        {
            InitializeComponent();
             R = (int) numericUpDownR.Value;
             G = (int)numericUpDownG.Value;
             B = (int)numericUpDownB.Value;
            A = 255;
            calculateHexColor();
        }

        private void btnPick_Click(object sender, EventArgs e)
        {
          
            clrPicker.Color = picColor.BackColor;     
  //PictureBox背景顏色設為 C olor Dialog預設顏色
            clrPicker.FullOpen = true;                  //開啟Color Dialog

            if (clrPicker.ShowDialog() == DialogResult.OK)
            {
                string HexColor = string.Format("0x{0:X8}", clrPicker.Color.ToArgb());
                txtHex.Text = "#" + HexColor.Substring(HexColor.Length - 6, 6);

                R = clrPicker.Color.R;
                G = clrPicker.Color.G;
                B = clrPicker.Color.B;
                numericUpDownR.Value = clrPicker.Color.R;
                numericUpDownG.Value = clrPicker.Color.G;
                numericUpDownB.Value = clrPicker.Color.B;
                picColor.BackColor = clrPicker.Color;
            }
        }

        private void numericUpDownR_ValueChanged(object sender, EventArgs e)
        {
            R = (int)numericUpDownR.Value;
            calculateHexColor();
        }

        private void numericUpDownG_ValueChanged(object sender, EventArgs e)
        {
            G = (int)numericUpDownG.Value;
            calculateHexColor();
        }

        private void numericUpDownB_ValueChanged(object sender, EventArgs e)
        {
            B = (int)numericUpDownB.Value;
            calculateHexColor();
        }
     
        private void calculateHexColor()
        {
            double LabL, Laba, Labb;
            picColor.BackColor = Color.FromArgb(A, R, G, B);
            clrPicker.Color = picColor.BackColor;
            HexColor = string.Format("0x{0:X8}", clrPicker.Color.ToArgb());
            txtHex.Text = "#" + HexColor.Substring(HexColor.Length - 6, 6);
            unsafe
            {
                colorspace.RGB2Lab(R, G, B, &LabL, &Laba, &Labb);
            }
            txtL.Text = String.Format("{0:F2}", LabL);
            txta.Text = String.Format("{0:F2}", Laba);
            txtb.Text = String.Format("{0:F2}", Labb);
        } 
    }
}

參考資料:

Getting the hex value of a color using ColorPicker

http://www.brucelindbloom.com

NumericUpDown

 [C#] RGB to Lab色彩轉換

 

春藥 2020-02-26 12:24:18

很不錯的分享~!

小風 2014-07-28 11:33:57

大大你好 這個轉換程式可否提供載點或是驅動 謝謝大哥 小弟e-mail amiya_lin@yahoo.com.tw