]> git.lyx.org Git - features.git/blobdiff - src/Color.h
Add a new Color class. This class makes it possible to specify a color as a merging...
[features.git] / src / Color.h
index 7270991f09fe35f461399675f0ae61d38908143f..ec1a2d9ba476aed689a4799e4c4dace52107270e 100644 (file)
 // -*- C++ -*-
 /**
- * \file color.h
+ * \file Color.h
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
+ * \author Asger Alstrup
+ * \author Lars Gullik Bjønnes
+ * \author Matthias Ettrich
+ * \author Jean-Marc Lasgouttes
  * \author Angus Leeming
+ * \author John Levon
+ * \author André Pönitz
+ * \author Martin Vermeer
  *
  * Full author contact details are available in file CREDITS.
  */
 
-/* structs RGBColor and HSVColor to enable simple conversion between
- * color spaces.
- */
+#ifndef LCOLOR_H
+#define LCOLOR_H
+
+#include "ColorCode.h"
 
-#ifndef COLOR_H
-#define COLOR_H
+#include "support/strfwd.h"
 
+#include <map>
 #include <string>
 
 namespace lyx {
 
-struct RGBColor;
-/// returns a string of form #rrggbb, given an RGBColor struct
-std::string const X11hexname(RGBColor const & col);
+/**
+ * \class Color
+ *
+ * A class holding a definition of a certain color.
+ *
+ * A color can be one of the following kinds:
+ *
+ * - a single color, then mergeColor = Color_ignore
+ * - a merged color, i.e. the average of the base and merge colors.
+ */
 
-struct HSVColor {
-       double h;
-       double s;
-       double v;
-       HSVColor() : h(0.0), s(0.0), v(0.0) {}
-       HSVColor(double hue, double sat, double val)
-               : h(hue), s(sat), v(val) {}
-       HSVColor(RGBColor const &);
-};
+class Color
+{
+public:
+       ///
+       Color(ColorCode base_color = Color_none);
+       
+       /// comparison operators.
+       //@{
+       bool operator==(Color const & color) const;
+       bool operator!=(Color const & color) const;
+       bool operator<(Color const & color) const;
+       bool operator<=(Color const & color) const;
+       //@}
 
-struct RGBColor {
-       unsigned int r;
-       unsigned int g;
-       unsigned int b;
-       RGBColor() : r(0), g(0), b(0) {}
-       RGBColor(unsigned int red, unsigned int green, unsigned int blue)
-               : r(red), g(green), b(blue) {}
-       RGBColor(HSVColor const &);
-       /// \param x11hexname is of the form "#ffa071"
-       RGBColor(std::string const & x11hexname);
+       /// the base color
+       ColorCode baseColor;
+       /// The color that is merged with the base color. Set
+       /// mergeColor to Color_ignore if no merging is wanted.
+       ColorCode mergeColor;
 };
 
-struct NamedColor : public RGBColor {
-       std::string lyxname;
-       std::string guiname;
-       NamedColor() : RGBColor() {}
-       NamedColor(std::string const & lyx, std::string const & gui,
-                  RGBColor const & c)
-               : RGBColor(c), lyxname(lyx), guiname(gui) {}
-       RGBColor const & color() const { return *this; }
-};
+std::ostream & operator<<(std::ostream & os, Color color);
 
-inline
-bool operator==(RGBColor const & c1, RGBColor const & c2)
-{
-       return (c1.r == c2.r && c1.g == c2.g && c1.b == c2.b);
-}
+
+/**
+ * \class ColorSet
+ *
+ * A class holding color definitions and associated names for
+ * LaTeX, X11, the GUI, and LyX internally.
+ *
+ * A color can be one of the following kinds:
+ *
+ * - A real, predefined color, such as black, white, red or green.
+ * - A logical color, such as no color, inherit, math
+ */
 
 
-inline
-bool operator!=(RGBColor const & c1, RGBColor const & c2)
+// made copyable for same reasons as LyXRC was made copyable. See there for
+// explanation.
+
+class ColorSet
 {
-       return !(c1 == c2);
-}
+public:
+       ///
+       ColorSet();
+
+       /** set the given LyX color to the color defined by the X11 name given
+        *  \returns true if successful.
+        */
+       bool setColor(ColorCode col, std::string const & x11name);
+
+       /** set the given LyX color to the color defined by the X11
+        *  name given \returns true if successful. A new color entry
+        *  is created if the color is unknown
+        */
+       bool setColor(std::string const & lyxname, std::string const & x11name);
+
+       /// Get the GUI name of \c color.
+       docstring const getGUIName(ColorCode c) const;
+
+       /// Get the X11 name of \c color.
+       std::string const getX11Name(ColorCode c) const;
+
+       /// Get the LaTeX name of \c color.
+       std::string const getLaTeXName(ColorCode c) const;
+
+       /// Get the LyX name of \c color.
+       std::string const getLyXName(ColorCode c) const;
+
+       /// \returns the ColorCode associated with the LyX name.
+       ColorCode getFromLyXName(std::string const & lyxname) const;
+       /// \returns the ColorCode associated with the LaTeX name.
+       ColorCode getFromLaTeXName(std::string const & latexname) const;
+
+private:
+       ///
+       void addColor(ColorCode c, std::string const & lyxname); 
+       ///
+       class Information {
+       public:
+               /// the name as it appears in the GUI
+               std::string guiname;
+               /// the name used in LaTeX
+               std::string latexname;
+               /// the name for X11
+               std::string x11name;
+               /// the name for LyX
+               std::string lyxname;
+       };
+
+       /// initialise a color entry
+       struct ColorEntry;
+       void fill(ColorEntry const & entry);
+
+       ///
+       typedef std::map<ColorCode, Information> InfoTab;
+       /// the table of color Information
+       InfoTab infotab;
+
+       typedef std::map<std::string, ColorCode> Transform;
+       /// the transform between LyX color name string and integer code.
+       Transform lyxcolors;
+       /// the transform between LaTeX color name string and integer code.
+       Transform latexcolors;
+};
+
+
+/// the current color definitions
+extern ColorSet lcolor;
+/// the system color definitions
+extern ColorSet system_lcolor;
+
+std::string const X11hexname(RGBColor const & col);
+RGBColor rgbFromHexName(std::string const & x11hexname);
 
 } // namespace lyx