migueldeicaza / XtermSharp

XTerm emulator as a .NET library
MIT License
161 stars 34 forks source link

24bit fg/bg color is not implemented and I haz questions... #78

Open tig opened 4 years ago

tig commented 4 years ago

https://github.com/migueldeicaza/XtermSharp/blob/master/XtermSharp/Terminal.cs#L683

        public int MatchColor (int r1, int g1, int b1)
        {
            throw new NotImplementedException ();
        }

The code in InputHandler.cs looks right:

                } else if (p == 38) {
                    // fg color 256
                    if (pars [i + 1] == 2) {
                        i += 2;
                        fg = terminal.MatchColor (
                            pars [i] & 0xff,
                            pars [i + 1] & 0xff,
                            pars [i + 2] & 0xff);
                        if (fg == -1)
                            fg = 0x1ff;
                        i += 2;
                    } else if (pars [i + 1] == 5) {
                        i += 2;
                        p = pars [i] & 0xff;
                        fg = p;
                    }
                } else if (p == 48) {

But without MatchColor implemented, 24bit color is not implemented. The spec is:

ESC[ 38;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB foreground color
ESC[ 48;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB background color

I tried a naïve try:

    public int MatchColor (int r1, int g1, int b1)
    {
            return (int)System.Drawing.Color.FromArgb(r1,g1, b1).ToArgb();
    }

I obviously don't know what I'm doing because this always returns 255 even though in the debugger I see my values for r1, g1, and b1 being passed in.

What is the value of the the int fg and int bg supposed to hold?

I built this too to help:

        public class XtermCharAttribute {
            public int Attribute { get; set; }

            public static XtermCharAttribute FromAttribute(int attribute) {
                return new XtermCharAttribute(attribute);
            }

            public XtermCharAttribute(int attribute) {
                Attribute = attribute;
            }

            public bool Bold => ((FLAGS)(Attribute >> 18)).HasFlag(FLAGS.BOLD);
            public bool Italic => ((FLAGS)(Attribute >> 18)).HasFlag(FLAGS.ITALIC);
            public bool Underline => ((FLAGS)(Attribute >> 18)).HasFlag(FLAGS.UNDERLINE);

            public System.Drawing.Color FgColor => System.Drawing.Color.FromArgb((Attribute >> 9) & 0x1ff);
            public System.Drawing.Color BgColor => System.Drawing.Color.FromArgb(Attribute & 0x1ff);
        }

What am I doing wrong/missing?

migueldeicaza commented 4 years ago

I recently implemented this in SwiftTerm - the good news is that you don’t need the MatchColor, that whole chunk of code is wrong.

Greg who used to be on the VSMac team was merging the improvements I was doing on SwiftTerm regularly until he moved on to another project.

migueldeicaza commented 4 years ago

This is the commit that got true color support to SwiftTerm:

https://github.com/migueldeicaza/SwiftTerm/pull/57