【问题标题】:How do I convert Color to Char?如何将颜色转换为字符?
【发布时间】:2015-11-21 02:19:20
【问题描述】:

我发现这段代码可以将 Unicode 字符转换为颜色,例如如果字符 A,它将使 rgb 0 8 4。 我将如何反转它以将颜色变成字符,例如如果 rgb 是 0 8 4 它会变成 A?

        const int RedMask = 0xF8;
        const int GreenMask = 0xF8;
        const int BlueMask = 0xFC;
        const int RedShift = 8;
        const int GreenShift = 3;
        const int BlueShift = 2;

        int val = ch;
        int r = (val >> RedShift) & RedMask;
        int g = (val >> GreenShift) & GreenMask;
        int b = (val << BlueShift) & BlueMask;

        Color clr = Color.FromArgb(r, g, b);

【问题讨论】:

    标签: c# unicode colors char


    【解决方案1】:

    您的 char 在 .Net 中的长度为 2 个字节
    所以假设它是 0xFFFF

    RedMask =   0b11111000    
    GreenMask = 0b11111000    
    BlueMask =  0b11111100
    

    让我们将最高有效位称为第一位(最左边)

    这就是我们获得红色值的方式

    Right shift by 8 bits. You get  0b11111111(11111111) <-- this number in parenthesis get pushed off    
                           mask it  0b11111000
                           result   0b11111000 <-- our 1st to 5th bit are preserved
    

    所以我们从 Red 1st-5th 得到 1st-5th 位

    这就是我们获得绿色价值的方式

    Right shift by 3 bits. You get 0b1111111111111 (111)
                           mask it 0b0000011111000
                            result 0b0000011111000 <-- That's our 6th to 10th bit.
    

    现在我们从绿色的第 1 到第 5 位得到第 6 到第 10 位

    最后是蓝色

    Left shift by 2 bits. You get (11) 0b111111111111111100
                          mask it      0b000000000011111100
                          You get      0b000000000011111100 <-- That's the rest of the bits here :-)
    

    我们从绿色的第 1-6 位得到第 11-16 位

    ==================================
    现在我们将所有这些放在一起,我们可以通过将红色的第 1-5 位拼接到绿色的第 1-5 位和蓝色的第 1-6 位来重构原始值。像这样:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Text;
    
    namespace WindowsFormsApplication1
    {
        static class Program
        {
    
            static void Main()
            {
                Color color = Char2Color('A');
                char ch = Color2Char(color);
    
            }
    
            static char Color2Char(Color c)
            {
                Byte[] result = {0x00,0x00};
    
                var red = (c.R >> 3) ;
                var green = (c.G >> 3) ;
                var blue = (c.B >> 2);
    
                result[0] = (byte)(red << 3);
                result[0] = (byte)(result[0] + (green >> 2));
                result[1] = (byte)(green << 6);
                result[1] = (byte)(result[1] + blue);
                Array.Reverse(result);
                return BitConverter.ToChar(result,0);
            }
    
            static Color Char2Color(char ch)
            {
                const int RedMask = 0xF8;
                const int GreenMask = 0xF8;
                const int BlueMask = 0xFC;
                const int RedShift = 8;
                const int GreenShift = 3;
                const int BlueShift = 2;
    
                int val = ch;
                int r = (val >> RedShift) & RedMask;
                int g = (val >> GreenShift) & GreenMask;
                int b = (val << BlueShift) & BlueMask;
    
                return Color.FromArgb(r, g, b);
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 2019-01-01
      • 1970-01-01
      • 2012-11-01
      相关资源
      最近更新 更多