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
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;
28 Concrete::Gray::setStroking( std::ostream
& os
) const
38 Concrete::Gray::setNonStroking( std::ostream
& os
) const
47 RefCountPtr
< SimplePDF::PDF_Vector
>
48 Concrete::Gray::componentVector( ) const
50 return RefCountPtr
< SimplePDF::PDF_Vector
>( new SimplePDF::PDF_Vector( gr_
) );
54 Concrete::Gray::add( const Concrete::Gray
& col2
, const Ast::SourceLocation
& callLoc
) const
56 double res_gr
= gr_
+ col2
.gr_
;
59 if( res_gr
- 1 > theFloatTol
)
61 throw Exceptions::OutOfRange( callLoc
, strrefdup( "The sum is greater than 1 in the gray component." ) );
68 return Concrete::Gray( res_gr
);
72 Concrete::Gray::mul( double factor
, const Ast::SourceLocation
& factorLoc
) const
76 throw Exceptions::OutOfRange( factorLoc
, strrefdup( "The scalar is less than 0." ) );
80 throw Exceptions::OutOfRange( factorLoc
, strrefdup( "The scalar is greater than 1." ) );
82 return Concrete::Gray( factor
* gr_
);
88 Concrete::RGB::setStroking( std::ostream
& os
) const
90 os
<< r_
<< " " << g_
<< " " << b_
<< " RG " ;
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_
) );
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_
;
113 if( res_r
- 1 > theFloatTol
)
115 throw Exceptions::OutOfRange( callLoc
, strrefdup( "The sum is greater than 1 in the red component." ) );
124 if( res_g
- 1 > theFloatTol
)
126 throw Exceptions::OutOfRange( callLoc
, strrefdup( "The sum is greater than 1 in the green component." ) );
135 if( res_b
- 1 > theFloatTol
)
137 throw Exceptions::OutOfRange( callLoc
, strrefdup( "The sum is greater than 1 in the blue component." ) );
144 return Concrete::RGB( res_r
, res_g
, res_b
);
148 Concrete::RGB::mul( double factor
, const Ast::SourceLocation
& factorLoc
) const
152 throw Exceptions::OutOfRange( factorLoc
, strrefdup( "The scalar is less than 0." ) );
156 throw Exceptions::OutOfRange( factorLoc
, strrefdup( "The scalar is greater than 1." ) );
158 return Concrete::RGB( factor
* r_
, factor
* g_
, factor
* b_
);
163 Concrete::CMYK::setStroking( std::ostream
& os
) const
165 os
<< c_
<< " " << m_
<< " " << y_
<< " " << k_
<< " K " ;
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_
) );
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_
;
190 if( res_k
- 1 > theFloatTol
)
192 throw Exceptions::OutOfRange( callLoc
, strrefdup( "The sum is greater than 1 in the black component." ) );
196 res_c
= res_m
= res_y
= 0;
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." ) );
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." ) );
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." ) );
236 return Concrete::CMYK( res_c
, res_m
, res_y
, res_k
);
241 Concrete::CMYK::mul( double factor
, const Ast::SourceLocation
& factorLoc
) const
245 throw Exceptions::OutOfRange( factorLoc
, strrefdup( "The scalar is less than 0." ) );
249 throw Exceptions::OutOfRange( factorLoc
, strrefdup( "The scalar is greater than 1." ) );
251 return Concrete::CMYK( factor
* c_
, factor
* m_
, factor
* y_
, factor
* k_
);