inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_scolor 00005 #define inlib_scolor 00006 00007 #include "colord" 00008 00009 #include "smanip" //for values<> 00010 #include <cstdio> //::sscanf 00011 00012 namespace inlib { 00013 00014 inline bool to_rgb(const std::string& a_string,double& a_r,double& a_g,double& a_b){ 00015 a_r = 0.5; 00016 a_g = 0.5; 00017 a_b = 0.5; 00018 if(a_string.empty()) return false; 00019 std::vector<double> ds; 00020 if(values<double>(a_string," ",false,ds) && (ds.size()>=3)) { 00021 if( ds[0]<0 || ds[0]>1 || 00022 ds[1]<0 || ds[1]>1 || 00023 ds[2]<0 || ds[2]>1 ) return false; 00024 a_r = ds[0]; 00025 a_g = ds[1]; 00026 a_b = ds[2]; 00027 return true; 00028 } else if( (a_string.size()==7) && (a_string[0]=='#') ) { 00029 // #RRGGBB format : 00030 // 1 3 5 00031 unsigned int rr; 00032 if(::sscanf(a_string.substr(1,2).c_str(),"%x",&rr)!=1) return false; 00033 unsigned int gg; 00034 if(::sscanf(a_string.substr(3,2).c_str(),"%x",&gg)!=1) return false; 00035 unsigned int bb; 00036 if(::sscanf(a_string.substr(5,2).c_str(),"%x",&bb)!=1) return false; 00037 a_r = double(rr)/255.; 00038 a_g = double(gg)/255.; 00039 a_b = double(bb)/255.; 00040 return true; 00041 } else { 00042 return colord::to(a_string,a_r,a_g,a_b); 00043 } 00044 } 00045 00046 inline bool to_rgba(const std::string& a_string,double& a_r,double& a_g,double& a_b,double& a_a){ 00047 a_r = 0.5; 00048 a_g = 0.5; 00049 a_b = 0.5; 00050 a_a = 0; 00051 if(a_string.empty()) return false; 00052 std::vector<double> ds; 00053 if(values<double>(a_string," ",false,ds) && (ds.size()>=4)) { 00054 if( ds[0]<0 || ds[0]>1 || 00055 ds[1]<0 || ds[1]>1 || 00056 ds[2]<0 || ds[2]>1 || 00057 ds[3]<0 || ds[3]>1 ) return false; 00058 a_r = ds[0]; 00059 a_g = ds[1]; 00060 a_b = ds[2]; 00061 a_a = ds[3]; 00062 return true; 00063 } else { 00064 return to_rgb(a_string,a_r,a_g,a_b); 00065 } 00066 } 00067 00068 inline bool to_rgbs(const std::string& a_string,std::string& a_value) { 00069 double r,g,b; 00070 if(!to_rgb(a_string,r,g,b)) return false; 00071 a_value = to<double>(r); 00072 a_value += " "; 00073 a_value = to<double>(g); 00074 a_value += " "; 00075 a_value = to<double>(b); 00076 return true; 00077 } 00078 00079 } 00080 00081 #endif