Jump to content
Welcome, welcome! Come in and register, and have some developer coffee. 👨‍💻 ×

Convert 24-bit RGB to 4-bit Console Color


Recommended Posts

     static int SimplifyColorComponent(int Value)
       {
           if (Value >= 52)
               return 63;
           else if (Value >= 32)
               return 42;
           else if (Value >= 12)
               return 21;

           return 0;
       }

       static byte DecreaseColor256(byte Red, byte Green, byte Blue)
       {
           byte[,] Palette16 = new byte[16,3] { {0, 0, 0 }, {0, 0, 42 }, {0, 42, 0 }, {0, 42, 42 }, { 42, 0, 0 }, {42, 0, 42}, {42, 42, 0}, {42, 42, 42}, { 0, 0, 21}, {0, 0, 63}, {0, 42, 21}, {0, 42, 63}, { 42, 0, 21}, { 42, 0, 63}, { 42, 42, 21}, { 42, 42, 63} };

           byte Color;
           byte Component;
           byte Value = 0;
           byte NewRed = 0;
           byte NewGreen = 0;
           byte NewBlue = 0;

           for (Component = 0; Component <= 2; Component++)
           {
               if (Component == 0)
                   Value = (byte)SimplifyColorComponent(Red / 4);
               else if (Component == 1)
                   Value = (byte)SimplifyColorComponent(Green / 4);
               else if (Component == 2)
                   Value = (byte)SimplifyColorComponent(Blue / 4);

               Color = 0;

               while (Value != Palette16[Color, Component])
               {
                   Color++;

                   if (Color > 15)
                   {
                       Value -= 21;
                       Color = 0;
                   }
               }

               if (Component == 0)
                   NewRed = Value;
               else if (Component == 1)
                   NewGreen = Value;
               else if (Component == 2)
                   NewBlue = Value;

           }

           for (Color = 0; Color <= 15; Color++)
               if ((Palette16[Color, 0] == NewRed) && (Palette16[Color, 1] == NewGreen) && (Palette16[Color, 2] == NewBlue))
                   return Color;

           return 0;
       }          

DecreaseColor256(253, 250, 249) = 15

Your console window will understand the color value of the returned color code.

 

  • Love 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...