Update procedures
[shapes.git] / source / concretecolors.cc
blob89fef287f7c0d7febbcfd9c012df5710c5849af7
1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * any later version.
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2008 Henrik Tidefelt
19 #include "concretecolors.h"
20 #include "shapesexceptions.h"
21 #include "sourcelocation.h"
23 using namespace Shapes;
25 const double theFloatTol = 0.001;
27 void
28 Concrete::Gray::setStroking( std::ostream & os ) const
30 if( gr_ < 0 )
32 return;
34 os << gr_ << " G " ;
37 void
38 Concrete::Gray::setNonStroking( std::ostream & os ) const
40 if( gr_ < 0 )
42 return;
44 os << gr_ << " g " ;
47 RefCountPtr< SimplePDF::PDF_Vector >
48 Concrete::Gray::componentVector( ) const
50 return RefCountPtr< SimplePDF::PDF_Vector >( new SimplePDF::PDF_Vector( gr_ ) );
53 Concrete::Gray
54 Concrete::Gray::add( const Concrete::Gray & col2, const Ast::SourceLocation & callLoc ) const
56 double res_gr = gr_ + col2.gr_;
57 if( res_gr > 1 )
59 if( res_gr - 1 > theFloatTol )
61 throw Exceptions::OutOfRange( callLoc, strrefdup( "The sum is greater than 1 in the gray component." ) );
63 else
65 res_gr = 1;
68 return Concrete::Gray( res_gr );
71 Concrete::Gray
72 Concrete::Gray::mul( double factor, const Ast::SourceLocation & factorLoc ) const
74 if( factor < 0 )
76 throw Exceptions::OutOfRange( factorLoc, strrefdup( "The scalar is less than 0." ) );
78 if( factor > 1 )
80 throw Exceptions::OutOfRange( factorLoc, strrefdup( "The scalar is greater than 1." ) );
82 return Concrete::Gray( factor * gr_ );
87 void
88 Concrete::RGB::setStroking( std::ostream & os ) const
90 os << r_ << " " << g_ << " " << b_ << " RG " ;
93 void
94 Concrete::RGB::setNonStroking( std::ostream & os ) const
96 os << r_ << " " << g_ << " " << b_ << " rg " ;
99 RefCountPtr< SimplePDF::PDF_Vector >
100 Concrete::RGB::componentVector( ) const
102 return RefCountPtr< SimplePDF::PDF_Vector >( new SimplePDF::PDF_Vector( r_, g_, b_ ) );
105 Concrete::RGB
106 Concrete::RGB::add( const Concrete::RGB & col2, const Ast::SourceLocation & callLoc ) const
108 double res_r = r_ + col2.r_;
109 double res_g = g_ + col2.g_;
110 double res_b = b_ + col2.b_;
111 if( res_r > 1 )
113 if( res_r - 1 > theFloatTol )
115 throw Exceptions::OutOfRange( callLoc, strrefdup( "The sum is greater than 1 in the red component." ) );
117 else
119 res_r = 1;
122 if( res_g > 1 )
124 if( res_g - 1 > theFloatTol )
126 throw Exceptions::OutOfRange( callLoc, strrefdup( "The sum is greater than 1 in the green component." ) );
128 else
130 res_g = 1;
133 if( res_b > 1 )
135 if( res_b - 1 > theFloatTol )
137 throw Exceptions::OutOfRange( callLoc, strrefdup( "The sum is greater than 1 in the blue component." ) );
139 else
141 res_b = 1;
144 return Concrete::RGB( res_r, res_g, res_b );
147 Concrete::RGB
148 Concrete::RGB::mul( double factor, const Ast::SourceLocation & factorLoc ) const
150 if( factor < 0 )
152 throw Exceptions::OutOfRange( factorLoc, strrefdup( "The scalar is less than 0." ) );
154 if( factor > 1 )
156 throw Exceptions::OutOfRange( factorLoc, strrefdup( "The scalar is greater than 1." ) );
158 return Concrete::RGB( factor * r_, factor * g_, factor * b_ );
162 void
163 Concrete::CMYK::setStroking( std::ostream & os ) const
165 os << c_ << " " << m_ << " " << y_ << " " << k_ << " K " ;
168 void
169 Concrete::CMYK::setNonStroking( std::ostream & os ) const
171 os << c_ << " " << m_ << " " << y_ << " " << k_ << " k " ;
174 RefCountPtr< SimplePDF::PDF_Vector >
175 Concrete::CMYK::componentVector( ) const
177 return RefCountPtr< SimplePDF::PDF_Vector >( new SimplePDF::PDF_Vector( c_, m_, y_, k_ ) );
181 Concrete::CMYK
182 Concrete::CMYK::add( const CMYK & col2, const Ast::SourceLocation & callLoc ) const
184 double res_c = c_ + col2.c_;
185 double res_m = m_ + col2.m_;
186 double res_y = y_ + col2.y_;
187 double res_k = k_ + col2.k_;
188 if( res_k > 1 )
190 if( res_k - 1 > theFloatTol )
192 throw Exceptions::OutOfRange( callLoc, strrefdup( "The sum is greater than 1 in the black component." ) );
194 else
196 res_c = res_m = res_y = 0;
197 res_k = 1;
200 else
202 if( double tmp = res_c + res_k > 1 )
204 if( tmp - 1 > theFloatTol )
206 throw Exceptions::OutOfRange( callLoc, strrefdup( "The sum is greater than 1 in the cyan component." ) );
208 else
210 res_c = 1 - res_k;
213 if( double tmp = res_m + res_k > 1 )
215 if( tmp - 1 > theFloatTol )
217 throw Exceptions::OutOfRange( callLoc, strrefdup( "The sum is greater than 1 in the magenta component." ) );
219 else
221 res_m = 1 - res_k;
224 if( double tmp = res_y + res_k > 1 )
226 if( tmp - 1 > theFloatTol )
228 throw Exceptions::OutOfRange( callLoc, strrefdup( "The sum is greater than 1 in the yellow component." ) );
230 else
232 res_y = 1 - res_k;
236 return Concrete::CMYK( res_c, res_m, res_y, res_k );
240 Concrete::CMYK
241 Concrete::CMYK::mul( double factor, const Ast::SourceLocation & factorLoc ) const
243 if( factor < 0 )
245 throw Exceptions::OutOfRange( factorLoc, strrefdup( "The scalar is less than 0." ) );
247 if( factor > 1 )
249 throw Exceptions::OutOfRange( factorLoc, strrefdup( "The scalar is greater than 1." ) );
251 return Concrete::CMYK( factor * c_, factor * m_, factor * y_, factor * k_ );