]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/Color.C
Tiny clean-ups.
[lyx.git] / src / frontends / xforms / Color.C
index ba09d4d833da983631766f85de37cc65d39e605c..fdb6ed963b7d84066bd8fc99e1970dd034695f8c 100644 (file)
-// -*- C++ -*-
-/* This file is part of
- * ======================================================
- * 
- *           LyX, The Document Processor
- *      
- *         Copyright 1995 Matthias Ettrich
- *          Copyright 1995-2000 The LyX Team.
+/**
+ * \file Color.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *======================================================*/
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
+ */
 
 #include <config.h>
-#include FORMS_H_LOCATION
-
-#ifdef __GNUG_
-#pragma implementation
-#endif
 
-#include <algorithm> // max
-#include <cmath> // floor
-#include <fstream> // ofstream
 #include "Color.h"
-#include "lyxlex.h"
+
+#include "lyx_forms.h"
+
+#include "LColor.h"
+
+#include "support/std_sstream.h"
+
+#include <cmath>
+#include <iomanip>
+
+
+#ifndef CXX_GLOBAL_CSTD
+using std::floor;
+#endif
 
 using std::max;
 using std::min;
-using std::ofstream;
+using std::setw;
+
+using std::istringstream;
+using std::ostringstream;
+using std::string;
+
+
+namespace {
+
+int const nohue = -1;
+
+int hexstrToInt(string const & str)
+{
+        int val = 0;
+        istringstream is(str);
+        is >> std::setbase(16) >> val;
+        return val;
+}
 
-static int const nohue = -1;
+} // namespace anon
 
-RGBColor::RGBColor( HSVColor const & hsv )
+
+
+bool getRGBColor(LColor_color col,
+                unsigned int & r, unsigned int & g, unsigned int & b)
+{
+       string const name = lcolor.getX11Name(col);
+       Display * const display = fl_get_display();
+       Colormap const cmap = fl_state[fl_get_vclass()].colormap;
+       XColor xcol, ccol;
+
+       if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
+               r = 0;
+               g = 0;
+               b = 0;
+               return false;
+       }
+
+       r = xcol.red   / 256;
+       g = xcol.green / 256;
+       b = xcol.blue  / 256;
+       return true;
+}
+
+
+string const X11hexname(RGBColor const & col)
+{
+       ostringstream ostr;
+
+       ostr << '#' << std::setbase(16) << std::setfill('0')
+            << setw(2) << col.r
+            << setw(2) << col.g
+            << setw(2) << col.b;
+
+       return ostr.str();
+}
+
+
+RGBColor::RGBColor(string const & x11hexname)
+       : r(0), g(0), b(0)
+{
+       BOOST_ASSERT(x11hexname.size() == 7 && x11hexname[0] == '#');
+       r = hexstrToInt(x11hexname.substr(1,2));
+       g = hexstrToInt(x11hexname.substr(3,2));
+       b = hexstrToInt(x11hexname.substr(5,2));
+}
+
+
+RGBColor::RGBColor(HSVColor const & hsv)
 {
        double h = hsv.h;
-       double s = hsv.s;
-       double v = hsv.v;
-       
+       double const s = hsv.s;
+       double const v = hsv.v;
+
        double rd, gd, bd;
-       
-       if( h == nohue || s == 0.0 ) {
+
+       if (h == nohue || s == 0.0) {
                rd = gd = bd = v;
        } else {
-               if( h == 360.0 ) h = 0.0;
+               if (h == 360.0) h = 0.0;
                h /= 60.0;
 
-               int j = static_cast<int>( ::floor(h) );
-               if( j < 0 ) j = 0;
+               int const j = max(0, static_cast<int>(::floor(h)));
+               //if (j < 0) j = 0;
 
-               double f = h - j;
-               double p = v * (1.0 - s);
-               double q = v * (1.0 - (s*f));
-               double t = v * (1.0 - (s*(1.0 - f)));
+               double const f = h - j;
+               double const p = v * (1.0 - s);
+               double const q = v * (1.0 - (s * f));
+               double const t = v * (1.0 - (s * (1.0 - f)));
 
-               switch( j ) {
+               switch (j) {
                case 0:
                        rd = v;
                        gd = t;
@@ -89,134 +157,44 @@ RGBColor::RGBColor( HSVColor const & hsv )
                }
        }
 
-       r = static_cast<int>( ::floor((rd * 255.0) + 0.5) );
-       g = static_cast<int>( ::floor((gd * 255.0) + 0.5) );
-       b = static_cast<int>( ::floor((bd * 255.0) + 0.5) );
+       r = static_cast<int>(::floor((rd * 255.0) + 0.5));
+       g = static_cast<int>(::floor((gd * 255.0) + 0.5));
+       b = static_cast<int>(::floor((bd * 255.0) + 0.5));
 }
 
 
-HSVColor::HSVColor( RGBColor const & rgb )
+HSVColor::HSVColor(RGBColor const & rgb)
 {
-       // r, g, b lie in the range 0-1, not 0-255.
-       double r = rgb.r / 255.0;
-       double g = rgb.g / 255.0;
-       double b = rgb.b / 255.0;
+       double const r = rgb.r / 255.0;
+       double const g = rgb.g / 255.0;
+       double const b = rgb.b / 255.0;
 
-       double maxval = max( max( r, g ), b );
-       double minval = max( min( r, g ), b );
+       double const maxval = max(max(r, g), b);
+       double const minval = min(min(r, g), b);
 
        v = maxval;
 
-       double diff = maxval - minval;
-       if( maxval != 0.0 )
+       double const diff = maxval - minval;
+       if (maxval != 0.0)
                s = diff / maxval;
        else
                s = 0.0;
 
        h = nohue;
-       if( s != 0.0 ) {
-               double rc = (maxval - r) / diff;
-               double gc = (maxval - g) / diff;
-               double bc = (maxval - b) / diff;
+       if (s != 0.0) {
+               double const rc = (maxval - r) / diff;
+               double const gc = (maxval - g) / diff;
+               double const bc = (maxval - b) / diff;
 
-               if( r == maxval )
+               if (r == maxval)
                        h = bc - gc;
-               else if( g == maxval )
+               else if (g == maxval)
                        h = 2.0 + rc - bc;
-               else if( b == maxval )
+               else if (b == maxval)
                        h = 4.0 + gc - rc;
 
                h *= 60.0;
-               if ( h < 0 )
+               if (h < 0)
                        h += 360;
        }
 }
-
-
-// sorted by hand to prevent LyXLex from complaining on read().
-static
-keyword_item xformTags[] = {
-//     { "\\gui_active_tab", FL_LIGHTER_COL1 },
-       { "\\gui_background", FL_COL1 },
-       { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
-       { "\\gui_buttonleft", FL_LEFT_BCOL },
-       { "\\gui_buttonright", FL_RIGHT_BCOL },
-       { "\\gui_buttontop", FL_TOP_BCOL },
-       { "\\gui_inactive", FL_INACTIVE },
-       { "\\gui_push_button", FL_YELLOW },
-       { "\\gui_selected", FL_MCOL },  
-       { "\\gui_text", FL_BLACK }
-};
-
-
-static const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
-
-
-bool XformColor::read(string const & filename)
-{
-       LyXLex lexrc( xformTags, xformCount );
-       if( !lexrc.setFile( filename ) )
-               return false;
-
-       while( lexrc.IsOK() ) {
-               int le = lexrc.lex();
-
-               switch( le ) {
-               case LyXLex::LEX_UNDEF:
-                       lexrc.printError("Unknown tag `$$Token'");
-                       continue; 
-               case LyXLex::LEX_FEOF:
-                       continue;
-               default: break;
-               }
-
-               RGBColor col;
-
-               if( !lexrc.next() ) break;
-               col.r = lexrc.GetInteger();
-
-               if( !lexrc.next() ) break;
-               col.g = lexrc.GetInteger();
-
-               if( !lexrc.next() ) break;
-               col.b = lexrc.GetInteger();
-
-               fl_mapcolor(le, col.r, col.g, col.b);
-       }
-       
-       return true;
-}
-
-
-bool XformColor::write(string const & filename)
-{
-       ofstream os(filename.c_str());
-       if (!os)
-               return false;
-
-       os << "### This file is part of\n"
-          << "### ========================================================\n"
-          << "###          LyX, The Document Processor\n"
-          << "###\n"
-          << "###          Copyright 1995 Matthias Ettrich\n"
-          << "###          Copyright 1995-2000 The LyX Team.\n"
-          << "###\n"
-          << "### ========================================================\n"
-          << "\n"
-          << "# This file is written by LyX, if you want to make your own\n"
-          << "# modifications you should do them from inside LyX and save\n"
-          << "\n";
-
-       for( int i = 0; i < xformCount; ++i ) {
-               string tag  = xformTags[i].tag;
-               int colorID = xformTags[i].code;
-               RGBColor color;
-
-               fl_getmcolor(colorID, &color.r, &color.g, &color.b);
-
-               os << tag + " "
-                  << color.r << " " << color.g << " " << color.b << "\n";
-       }
-
-       return true;
-}