Make \color work as a switch, as in LaTeX, and add the other features from the color package (\definecolor, other color models, LaTeX named colors, \colorbox, \fcolorbox, etc.). (Resolves issue #38.)

This commit is contained in:
Davide P. Cervone 2011-04-24 18:23:21 -04:00
parent bb09608b23
commit 4b2d568822
2 changed files with 239 additions and 0 deletions

16
extensions/TeX/color.js Normal file
View File

@ -0,0 +1,16 @@
/*
* /MathJax/extensions/TeX/color.js
*
* Copyright (c) 2010 Design Science, Inc.
*
* Part of the MathJax library.
* See http://www.mathjax.org for details.
*
* Licensed under the Apache License, Version 2.0;
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
MathJax.Extension["TeX/color"]={version:"1.1",colors:{Apricot:"#FBB982",Aquamarine:"#00B5BE",Bittersweet:"#C04F17",Black:"#221E1F",Blue:"#2D2F92",BlueGreen:"#00B3B8",BlueViolet:"#473992",BrickRed:"#B6321C",Brown:"#792500",BurntOrange:"#F7921D",CadetBlue:"#74729A",CarnationPink:"#F282B4",Cerulean:"#00A2E3",CornflowerBlue:"#41B0E4",Cyan:"#00AEEF",Dandelion:"#FDBC42",DarkOrchid:"#A4538A",Emerald:"#00A99D",ForestGreen:"#009B55",Fuchsia:"#8C368C",Goldenrod:"#FFDF42",Gray:"#949698",Green:"#00A64F",GreenYellow:"#DFE674",JungleGreen:"#00A99A",Lavender:"#F49EC4",LimeGreen:"#8DC73E",Magenta:"#EC008C",Mahogany:"#A9341F",Maroon:"#AF3235",Melon:"#F89E7B",MidnightBlue:"#006795",Mulberry:"#A93C93",NavyBlue:"#006EB8",OliveGreen:"#3C8031",Orange:"#F58137",OrangeRed:"#ED135A",Orchid:"#AF72B0",Peach:"#F7965A",Periwinkle:"#7977B8",PineGreen:"#008B72",Plum:"#92268F",ProcessBlue:"#00B0F0",Purple:"#99479B",RawSienna:"#974006",Red:"#ED1B23",RedOrange:"#F26035",RedViolet:"#A1246B",Rhodamine:"#EF559F",RoyalBlue:"#0071BC",RoyalPurple:"#613F99",RubineRed:"#ED017D",Salmon:"#F69289",SeaGreen:"#3FBC9D",Sepia:"#671800",SkyBlue:"#46C5DD",SpringGreen:"#C6DC67",Tan:"#DA9D76",TealBlue:"#00AEB3",Thistle:"#D883B7",Turquoise:"#00B4CE",Violet:"#58429B",VioletRed:"#EF58A0",White:"#FFFFFF",WildStrawberry:"#EE2967",Yellow:"#FFF200",YellowGreen:"#98CC70",YellowOrange:"#FAA21A"},getColor:function(a,c){if(!a){a="named"}var b=this["get_"+a];if(!b){this.TEX.Error("Color model '"+a+"' not defined")}return b.call(this,c)},get_rgb:function(b){b=b.split(/,/);var a="#";if(b.length!==3){this.TEX.Error("RGB colors require 3 decimal numbers")}for(var c=0;c<3;c++){if(!b[c].match(/^(\d+(\.\d*)?|\.\d+)$/)){this.TEX.Error("Invalid decimal number")}var d=parseFloat(b[c]);if(d<0||d>1){this.TEX.Error("RGB values must be between 0 and 1")}d=Math.floor(d*255).toString(16);if(d.length<2){d="0"+d}a+=d}return a},get_gray:function(a){if(!a.match(/^(\d+(\.\d*)?|\.\d+)$/)){this.TEX.Error("Invalid decimal number")}var b=parseFloat(a);if(b<0||b>1){this.TEX.Error("Grey-scale values must be between 0 and 1")}b=Math.floor(b*255).toString(16);if(b.length<2){b="0"+b}return"#"+b+b+b},get_named:function(a){if(this.colors[a]){return this.colors[a]}return a}};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var d=MathJax.InputJax.TeX,a=MathJax.ElementJax.mml;var c=d.Stack.Item;var b=MathJax.Extension["TeX/color"];b.TEX=d;d.Definitions.macros.definecolor="DefineColor";d.Definitions.macros.colorbox="ColorBox";d.Definitions.macros.fcolorbox="fColorBox";d.Parse.Augment({Color:function(h){var g=this.GetBrackets(h),e=this.GetArgument(h);e=b.getColor(g,e);var f=c.style().With({styles:{mathcolor:e}});this.stack.env.color=e;this.Push(f)},DefineColor:function(g){var f=this.GetArgument(g),e=this.GetArgument(g),h=this.GetArgument(g);b.colors[f]=b.getColor(e,h)},ColorBox:function(g){var f=this.GetArgument(g),e=this.InternalMath(this.GetArgument(g));this.Push(a.mpadded.apply(a,e).With({mathbackground:b.getColor("named",f),width:"+10px",height:"+5px",depth:"+5px",lspace:"5px"}))},fColorBox:function(h){var i=this.GetArgument(h),g=this.GetArgument(h),e=this.InternalMath(this.GetArgument(h));var f=a.mpadded.apply(a,e).With({mathbackground:b.getColor("named",g),width:"+10px",height:"+5px",depth:"+5px",lspace:"5px"});this.Push(a.mpadded(f).With({mathbackground:b.getColor("named",i),width:"+4px",height:"+2px",depth:"+2px",lspace:"2px"}))}});MathJax.Hub.Startup.signal.Post("TeX color Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/color.js");

View File

@ -0,0 +1,223 @@
/*************************************************************
*
* MathJax/extensions/TeX/color.js
*
* Implements LaTeX-compatible \color macro rather than MathJax's
* original (non-standard) version. It includes the rgb, gray, and
* named color models, and the \definecolor macro.
*
* ---------------------------------------------------------------------
*
* Copyright (c) 2011 Design Science, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// The configuration defaults, augmented by the user settings
//
MathJax.Extension["TeX/color"] = {
version: "1.1",
colors: {
Apricot: "#FBB982",
Aquamarine: "#00B5BE",
Bittersweet: "#C04F17",
Black: "#221E1F",
Blue: "#2D2F92",
BlueGreen: "#00B3B8",
BlueViolet: "#473992",
BrickRed: "#B6321C",
Brown: "#792500",
BurntOrange: "#F7921D",
CadetBlue: "#74729A",
CarnationPink: "#F282B4",
Cerulean: "#00A2E3",
CornflowerBlue: "#41B0E4",
Cyan: "#00AEEF",
Dandelion: "#FDBC42",
DarkOrchid: "#A4538A",
Emerald: "#00A99D",
ForestGreen: "#009B55",
Fuchsia: "#8C368C",
Goldenrod: "#FFDF42",
Gray: "#949698",
Green: "#00A64F",
GreenYellow: "#DFE674",
JungleGreen: "#00A99A",
Lavender: "#F49EC4",
LimeGreen: "#8DC73E",
Magenta: "#EC008C",
Mahogany: "#A9341F",
Maroon: "#AF3235",
Melon: "#F89E7B",
MidnightBlue: "#006795",
Mulberry: "#A93C93",
NavyBlue: "#006EB8",
OliveGreen: "#3C8031",
Orange: "#F58137",
OrangeRed: "#ED135A",
Orchid: "#AF72B0",
Peach: "#F7965A",
Periwinkle: "#7977B8",
PineGreen: "#008B72",
Plum: "#92268F",
ProcessBlue: "#00B0F0",
Purple: "#99479B",
RawSienna: "#974006",
Red: "#ED1B23",
RedOrange: "#F26035",
RedViolet: "#A1246B",
Rhodamine: "#EF559F",
RoyalBlue: "#0071BC",
RoyalPurple: "#613F99",
RubineRed: "#ED017D",
Salmon: "#F69289",
SeaGreen: "#3FBC9D",
Sepia: "#671800",
SkyBlue: "#46C5DD",
SpringGreen: "#C6DC67",
Tan: "#DA9D76",
TealBlue: "#00AEB3",
Thistle: "#D883B7",
Turquoise: "#00B4CE",
Violet: "#58429B",
VioletRed: "#EF58A0",
White: "#FFFFFF",
WildStrawberry: "#EE2967",
Yellow: "#FFF200",
YellowGreen: "#98CC70",
YellowOrange: "#FAA21A"
},
/*
* Look up a color based on its model and definition
*/
getColor: function (model,def) {
if (!model) {model = "named"}
var fn = this["get_"+model];
if (!fn) {this.TEX.Error("Color model '"+model+"' not defined")}
return fn.call(this,def);
},
/*
* Get an RGB color
*/
get_rgb: function (rgb) {
rgb = rgb.split(/,/); var RGB = "#";
if (rgb.length !== 3) {this.TEX.Error("RGB colors require 3 decimal numbers")}
for (var i = 0; i < 3; i++) {
if (!rgb[i].match(/^(\d+(\.\d*)?|\.\d+)$/)) {this.TEX.Error("Invalid decimal number")}
var n = parseFloat(rgb[i]);
if (n < 0 || n > 1) {this.TEX.Error("RGB values must be between 0 and 1")}
n = Math.floor(n*255).toString(16); if (n.length < 2) {n = "0"+n}
RGB += n;
}
return RGB;
},
/*
* Get a gray-scale value
*/
get_gray: function (gray) {
if (!gray.match(/^(\d+(\.\d*)?|\.\d+)$/)) {this.TEX.Error("Invalid decimal number")}
var n = parseFloat(gray);
if (n < 0 || n > 1) {this.TEX.Error("Grey-scale values must be between 0 and 1")}
n = Math.floor(n*255).toString(16); if (n.length < 2) {n = "0"+n}
return "#"+n+n+n;
},
/*
* Get a named value
*/
get_named: function (name) {
if (this.colors[name]) {return this.colors[name]}
return name;
}
};
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
var TEX = MathJax.InputJax.TeX,
MML = MathJax.ElementJax.mml;
var STACKITEM = TEX.Stack.Item;
var COLOR = MathJax.Extension["TeX/color"];
COLOR.TEX = TEX; // for reference in getColor above
TEX.Definitions.macros.definecolor = "DefineColor";
TEX.Definitions.macros.colorbox = "ColorBox";
TEX.Definitions.macros.fcolorbox = "fColorBox";
TEX.Parse.Augment({
//
// Override \color macro definition
//
Color: function (name) {
var model = this.GetBrackets(name),
color = this.GetArgument(name);
color = COLOR.getColor(model,color);
var mml = STACKITEM.style().With({styles:{mathcolor:color}});
this.stack.env.color = color;
this.Push(mml);
},
//
// Define the \definecolor macro
//
DefineColor: function (name) {
var cname = this.GetArgument(name),
model = this.GetArgument(name),
def = this.GetArgument(name);
COLOR.colors[cname] = COLOR.getColor(model,def);
},
//
// Produce a text box with a colored background
//
ColorBox: function (name) {
var cname = this.GetArgument(name),
arg = this.InternalMath(this.GetArgument(name));
this.Push(MML.mpadded.apply(MML,arg).With({
mathbackground:COLOR.getColor("named",cname),
width: "+10px", height: "+5px", depth: "+5px", lspace: "5px"
}));
},
//
// Procude a framed text box with a colored background
//
fColorBox: function (name) {
var fname = this.GetArgument(name),
cname = this.GetArgument(name),
arg = this.InternalMath(this.GetArgument(name));
var mml = MML.mpadded.apply(MML,arg).With({
mathbackground: COLOR.getColor("named",cname),
width: "+10px", height: "+5px", depth: "+5px", lspace: "5px"
});
// *** FIXME: this should really be a style with border,
// but that doesn't work well in HTML-CSS at the moment
this.Push(MML.mpadded(mml).With({
mathbackground: COLOR.getColor("named",fname),
width: "+4px", height: "+2px", depth: "+2px", lspace: "2px"
}));
}
});
MathJax.Hub.Startup.signal.Post("TeX color Ready");
});
MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/color.js");