Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for color format #165

Closed
ForNeVeR opened this issue Oct 1, 2018 · 9 comments · Fixed by #142
Closed

Proposal for color format #165

ForNeVeR opened this issue Oct 1, 2018 · 9 comments · Fixed by #142

Comments

@ForNeVeR
Copy link
Owner

ForNeVeR commented Oct 1, 2018

While working on #142, I've checked internet for documentation about how other LaTeX-related tools deal with colors. I have not found any evidence that LaTeX supports direct color definition (e.g. \color{rgb}{1.0,1.5,1.0}{mytext}) for either \color or \colorbox. Although, I've found a useful piece of documentation on Wikibooks.

I don't want to add ability to define new colors in WPF-Math LaTeX documents, so I don't want to add commands such as \definecolor. I believe that the markup should stay static at the moment and should not have any influence on itself (i.e. no macro or color or whatever definitions in markup).

So I suggest that we document and add a modified syntax for \color and \colorbox commands by adding an optional mode parameter. Full syntax will be:

\command [mode] {color} {text}

Where:

  • command is either color or colorbox
  • mode is one of gray, rgb, RGB, HTML, cmyk (for now let's stop on that, but the code should be robust enough to add more modes: xcolor manual includes some additional modes)
  • color is either a predefined color name (as implemented already) if the mode argument wasn't provided or a proper color definition according to the wikibooks documentation
  • additionally to the standard color definitions, we will support a transparency parameter that will be optional and should be added in a convenient manner to every mode we support

The motivation for adding transparency is clear: while LaTeX was designed to provide mostly a printed documents, we know that we're working with display graphics in WPF-Math, and transparency could be useful and shouldn't be too hard to implement, so I think we should add it.

Here're some syntax samples:

\colorbox{red}{text} // already implemented, "red" is a predefined color name
\colorbox{red,0.5}{text} // predefined color with transparency
\colorbox[gray]{0.5}{text}
\colorbox[gray]{0.5, 0.5}{text} // added transparency; also note that embedded spaces should be supported
\colorbox[rgb]{0,0,0}{text}
\colorbox[rgb]{0,0,0, 0.5}{text}
\colorbox[RGB]{255,255,255}{text}
\colorbox[RGB]{255,255,255,0}{text}
\colorbox[HTML]{FFFFFF}{text}
\colorbox[HTML]{ffffff00}{text}
\colorbox[cmyk]{0,0,0,1}{text}
\colorbox[cmyk]{0,0,0,1,0.5}{text}
@alexreg
Copy link
Collaborator

alexreg commented Oct 1, 2018

This is a nice feature, and I like the general syntactical approach taken here.

@B3zaleel
Copy link
Contributor

B3zaleel commented Oct 2, 2018

Shouldn't transparent RGB be ARGB and transparent rgb, argb?

@ForNeVeR
Copy link
Owner Author

ForNeVeR commented Oct 2, 2018

Shouldn't transparent RGB be ARGB and transparent rgb, argb?

Yeah, that's a good question. I see two general strategies here:

  1. Add an transparent variant for each of the "standard" color schemes: RGB -> ARGB (or maybe RGBA?), gray -> agray (well, it's not very good), HTML -> AHTML (seriously, what)
  2. Keep each scheme intact and add an optional transparency parameter to the end of parameter list.

I don't like the strategy of ("randomly") adding our own color schemes on top of the standard ones, so I don't think that's a good idea to add argb and ARGB but don't touch e.g HTML. And I personally like to use HTML coloring in my markup, so I want transparency to be there.

TL;DR: I don't think we need to add ARGB or RGBA to the mix; let's keep the standard naming and extend each supported color scheme to accept an optional transparency parameter.

@B3zaleel
Copy link
Contributor

B3zaleel commented Oct 2, 2018

In my current version, the HTML model supports short hex(333), plain hex(34e869), and long hex(5567899f), gray and cmyk can have an optional alpha channel factor, the alpha channel coefficient for the rgb and RGB models only work if you rename the models as argb and ARGB.

@ForNeVeR
Copy link
Owner Author

ForNeVeR commented Oct 2, 2018

Well, we're discussing not our current implementation, but the specification, i.e. how we want the feature to be implemented. I've noted your opinion on the matter, thanks. But it won't be too hard to change the implementation if we want the feature to be implemented in other way.

I'm okay with either implementations, actually. But we need to choose what specification we want to adhere.

@B3zaleel
Copy link
Contributor

B3zaleel commented Oct 2, 2018

In the command processing function, we could first check for a color model and read the next two arguments.

  • If the color model is not null, parse the first argument as a color definition with the color model in a static "ColorUtilities" class. If it is null, try the following.
  • Try to get the value of the first argument from the predefined colors.
  • Try to modify the modify the first argument using the "Color utilities" class(if given "\color{red, 0.4}{Text}", get the predefined color, red and give it an alpha value of 0.4 ×255 "floored" ). Nothing can be done if all of the above fail.

@B3zaleel
Copy link
Contributor

B3zaleel commented Oct 2, 2018

Here's a code sample of the above(where "ColorUtilities" is a static utility class for processing colors).

...
                case "color":
                    {
                        //Command to change the foreground color
                        string colorModel = null;

                        if (position < value.Length && value[position] == leftBracketChar)
                        {
                            colorModel = ReadElementGroup(value, ref position, leftBracketChar, rightBracketChar).ToString();
                            SkipWhiteSpace(value, ref position);
                        }

                        var colorName = ReadElement(value, ref position).ToString().Trim();
                        var bodyValue = ReadElement(value, ref position);
                        var bodyFormula = this.Parse(bodyValue, formula.TextStyle);
                        source = value.Segment(start, position - start);

                        if (colorModel!=null)
                        {
                            var color = ColorUtilities.Parse(colorModel.Trim(), colorName.ToString());
                            return new StyledAtom(source, bodyFormula.RootAtom, null, new SolidColorBrush(color));
                        }
                        else if (predefinedColors.TryGetValue(colorName, out Color color))
                        {
                            return new StyledAtom(source, bodyFormula.RootAtom, null, new SolidColorBrush(color));
                        }
                        else if (ColorUtilities.TryModifyPredefinedColor(colorName,out Color modifiedColor))
                        {
                            return new StyledAtom(source, bodyFormula.RootAtom, null, new SolidColorBrush(modifiedColor));
                        }
                        else
                        {
                            throw new TexParseException($"Color \"{colorName}\" could neither be found nor converted.");
                        }
                    }
...

@ForNeVeR
Copy link
Owner Author

ForNeVeR commented Oct 2, 2018

@B3zaleel you've already implemented the core of the feature, I'll take care of the rest, don't bother yourself :)

@ForNeVeR
Copy link
Owner Author

ForNeVeR commented Oct 3, 2018

Alrighty, I've asked several people, and we had no overall consensus on ARGB vs RGBA. Thus I've decided:

  • we'll keep RGB and rgb as-is (i.e. no transparency)
  • we'll add four new modes: ARGB, argb, RGBA, rgba

ForNeVeR added a commit that referenced this issue Oct 3, 2018
@ForNeVeR ForNeVeR mentioned this issue Sep 1, 2019
13 tasks
ForNeVeR added a commit that referenced this issue Oct 19, 2019
ForNeVeR added a commit that referenced this issue Oct 20, 2019
ForNeVeR added a commit that referenced this issue Oct 20, 2019
ForNeVeR added a commit that referenced this issue Oct 20, 2019
ForNeVeR added a commit that referenced this issue Oct 20, 2019
ForNeVeR added a commit that referenced this issue Oct 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants