ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C#]16bit RGB Color
    알고리즘 2022. 1. 27. 11:16

    //Hex -> RGB

    // ex) 0x1234 -> 16bit RGB 색상표현

     

    //Dec -> RGB

    // ex) 12345 -> 16bit RGB 색상표현

     

    public void setRGBText(string sRGBText)
            {
                string sRGB;
                int iRGB;
                if (sRGBText.Length < 5) // Hex값 일 경우
                {
                    sRGB = sRGBText.PadLeft(4, '0');                
                    iRGB = int.Parse(sRGB, System.Globalization.NumberStyles.HexNumber);  // Hex to Int
                    sRGB = Convert.ToString(iRGB, 2).PadLeft(16, '0');
                }
                else // Dec값 일 경우
                {
                    iRGB = Convert.ToInt32(sRGBText);  // Dec to Int
                    sRGB = Convert.ToString(iRGB, 2).PadLeft(16, '0');
                }                        

                if (sRGB.Length == 16)
                {
                    string sRed = sRGB.Substring(0, 5);
                    string sGreen = sRGB.Substring(5, 6);
                    string sBlue = sRGB.Substring(11, 5);

                    byte bRed = Convert.ToByte(sRed, 2);
                    byte bGreen = Convert.ToByte(sGreen, 2);
                    byte bBlue = Convert.ToByte(sBlue, 2);

                    int iRed = bRed * 8;
                    int iGreen = bGreen * 4;
                    int iBlue = bBlue * 8;

                    //int iDec = iRed | iGreen | iBlue;
                    txtHexValue.Text = Convert.ToString(iRGB, 16).PadLeft(4, '0');
                    //txtHexValue.Text = iRGB.ToString().PadLeft(4, '0');
                    txtDecValue.Text = iRGB.ToString().PadLeft(5, '0');

                    trackBarRed.Value = iRed;
                    trackBarGreen.Value = iGreen;
                    trackBarBlue.Value = iBlue;

                    labelR.Text = iRed.ToString();
                    labelG.Text = iGreen.ToString();
                    labelB.Text = iBlue.ToString();

                    RGBcolorDialog.BackColor = Color.FromArgb(iRed, iGreen, iBlue);
                }            

            }

    728x90

    '알고리즘' 카테고리의 다른 글

    온도 Color 테이블 계산  (0) 2022.03.30

    댓글

Designed by Tistory.