namespace skyscraper5.Dvb.Subtitling.Model
{
    public class ClutFamily
    {
        public ClutFamily()
        {
            GenerateDefault8bitClut();
            GenerateDefault4bitClut();
            GenerateDefault2bitClut();
        }

        public Color[] _8bitClut { get; set; }
        public Color[] _4bitClut { get; set; }
        public Color[] _2bitClut { get; set; }
        

        private void GenerateDefault8bitClut()
        {
            _8bitClut = new Color[256];
            for (int i = 0; i < 256; i++)
            {
                int b1 = (i & 0x80) >> 7;
                int b2 = (i & 0x40) >> 6;
                int b3 = (i & 0x20) >> 5;
                int b4 = (i & 0x10) >> 4;
                int b5 = (i & 0x08) >> 3;
                int b6 = (i & 0x04) >> 2;
                int b7 = (i & 0x02) >> 1;
                int b8 = (i & 0x01);
                if (b1 == 0 && b5 == 0)
                {
                    if (b2 == 0 && b3 == 0 && b4 == 0)
                    {
                        if (b6 == 0 && b7 == 0 && b8 == 0)
                            _8bitClut[i].T = 255;
                        else
                        {
                            _8bitClut[i].Pr = (byte)(255 * b8);
                            _8bitClut[i].Y = (byte)(255 * b7);
                            _8bitClut[i].Pb = (byte)(255 * b6);
                            _8bitClut[i].T = 191;
                        }
                    }
                    else
                    {
                        _8bitClut[i].Pr = (byte)(84 * b8 + 170 * b4);
                        _8bitClut[i].Y = (byte)(84 * b7 + 170 * b3);
                        _8bitClut[i].Pb = (byte)(84 * b6 + 170 * b2);
                        _8bitClut[i].T = 0;
                    }
                }
                else if (b1 == 0 && b5 == 1)
                {
                    _8bitClut[i].Pr = (byte)(84 * b8 + 170 * b4);
                    _8bitClut[i].Y = (byte)(84 * b7 + 170 * b3);
                    _8bitClut[i].Pb = (byte)(84 * b6 + 170 * b2);
                    _8bitClut[i].T = 127;
                }
                else if (b1 == 1 && b5 == 0)
                {
                    _8bitClut[i].Pr = (byte)(42 * b8 + 84 * b4 + 127);
                    _8bitClut[i].Y = (byte)(42 * b7 + 84 * b3 + 127);
                    _8bitClut[i].Pb = (byte)(42 * b6 + 84 * b2 + 127);
                    _8bitClut[i].T = 0;
                }
                else if (b1 == 1 && b5 == 1)
                {
                    _8bitClut[i].Pr = (byte)(42 * b8 + 84 * b4);
                    _8bitClut[i].Y = (byte)(42 * b7 + 84 * b3);
                    _8bitClut[i].Pb = (byte)(42 * b6 + 84 * b2);
                    _8bitClut[i].T = 0;
                }
            }
        }

        private void GenerateDefault4bitClut()
        {
            _4bitClut = new Color[16];
            for (int i = 0; i < 15; i++)
            {
                int b1 = (i & 0x08) >> 3;
                int b2 = (i & 0x04) >> 2;
                int b3 = (i & 0x02) >> 1;
                int b4 = (i & 0x01);
                if (b1 == 0)
                {
                    if (b2 == 0 && b3 == 0 && b4 == 0)
                    {
                        _4bitClut[i].T = 255;
                    }
                    else
                    {
                        _4bitClut[i].Pr = (byte)(255 * b4);
                        _4bitClut[i].Y = (byte)(255 * b3);
                        _4bitClut[i].Pb = (byte)(255 * b2);
                        _4bitClut[i].T = 0;
                    }
                }
                else
                {
                    _4bitClut[i].Pr = (byte)(127 * b4);
                    _4bitClut[i].Y = (byte)(127 * b3);
                    _4bitClut[i].Pb = (byte)(127 * b2);
                    _4bitClut[i].T = 0;
                }
            }
        }

        private void GenerateDefault2bitClut()
        {
            _2bitClut = new Color[4];
            _2bitClut[0].T = 255;
            _2bitClut[1].Pb = 255;
            _2bitClut[1].Y = 255;
            _2bitClut[1].Pr = 255;
            _2bitClut[1].T = 0;
            _2bitClut[2].Pb = 0;
            _2bitClut[2].Y = 0;
            _2bitClut[2].Pr = 0;
            _2bitClut[2].T = 0;
            _2bitClut[3].Pb = 127;
            _2bitClut[3].Y = 127;
            _2bitClut[3].Pr = 127;
            _2bitClut[3].T = 0;
        }
    }
}
