27 #ifndef MLN_VALUE_RGB_HH
28 # define MLN_VALUE_RGB_HH
31 # include <mln/value/ops.hh>
33 # include <mln/fun/v2v/hsl_to_rgb.hh>
34 # include <mln/value/concept/vectorial.hh>
35 # include <mln/value/int_u.hh>
36 # include <mln/algebra/vec.hh>
38 # include <mln/value/internal/make_generic_name.hh>
40 # include <mln/convert/from_to.hh>
49 namespace value {
template <
unsigned n>
struct rgb; }
59 template <
typename T_rgb>
62 typedef f_hsl_to_rgb_< value::rgb<8> > f_hsl_to_rgb_3x8_t;
104 template <
typename H,
typename S,
typename L>
class hsl_;
115 template <
typename T,
unsigned m>
116 void from_to_(
const algebra::vec<3,T>& from, value::rgb<m>& to_);
119 template <
unsigned m>
120 void from_to_(
bool from, value::rgb<m>&
to);
123 template <
unsigned m>
124 void from_to_(
const value::int_u<m>& from, value::rgb<m>&
to);
127 template <
typename H,
typename S,
typename L>
128 void from_to_(
const value::hsl_<H,S,L>&, value::rgb<8>&
to);
135 template <
unsigned m>
136 void from_to_(
const value::rgb<m>& from,
bool&
to);
145 template <
unsigned n >
151 template <
unsigned n >
157 template <
unsigned n,
typename S >
163 template <
unsigned n,
typename S >
176 template <
unsigned n,
unsigned m >
182 template <
unsigned n,
unsigned m >
205 template <
unsigned n>
206 struct value_< mln::value::rgb<n> >
211 card = mln_value_card_from_(nbits)
214 typedef trait::value::nature::vectorial nature;
215 typedef trait::value::kind::color kind;
216 typedef trait::value::quant::high quant;
223 template <
typename V>
static comp_0 get_comp_0(
const V& v) {
return v.red(); }
224 template <
typename V>
static comp_1 get_comp_1(
const V& v) {
return v.green(); }
225 template <
typename V>
static comp_2 get_comp_2(
const V& v) {
return v.blue(); }
227 typedef algebra::vec<dim, float>
sum;
229 static const char* name()
232 s = mln::value::internal::make_generic_name(
"rgb", n);
247 template <
unsigned n>
250 public Vectorial< rgb<n> >
252 public internal::value_like_< algebra::vec< 3, int_u<n> >,
253 algebra::vec< 3, int_u<n> >,
254 algebra::vec< 3, int >,
267 int_u<n> green()
const {
return this->v_[1]; }
268 int_u<n>& green() {
return this->v_[1]; }
270 int_u<n> blue()
const {
return this->v_[2]; }
271 int_u<n>& blue() {
return this->v_[2]; }
273 int_u<n> comp(
unsigned k)
const {
return this->v_[k]; }
274 int_u<n>& comp(
unsigned k) {
return this->v_[k]; }
281 rgb<n>(
int r,
int g,
int b);
284 rgb<n>(
const algebra::vec<3, int>& rhs);
285 rgb<n>(
const algebra::vec<3, unsigned>& rhs);
286 rgb<n>(
const algebra::vec<3, int_u<n> >& rhs);
287 rgb<n>(
const algebra::vec<3, float>& rhs);
290 operator algebra::vec<3, int>()
const {
return this->v_; }
292 operator algebra::vec<3, float>()
const {
return this->v_; }
299 rgb<n>(
const mln::literal::medium_gray_t&);
300 rgb<n>(
const mln::literal::dark_gray_t&);
333 template <
unsigned n>
334 std::ostream& operator<<(std::ostream& ostr, const rgb<n>& c);
336 template <
unsigned n>
337 std::istream& operator>>(std::istream& istr,
rgb<n>& c);
349 template <
unsigned n>
353 template <
unsigned n>
357 template <
unsigned n>
364 template <
unsigned n>
368 template <
unsigned n>
372 template <
unsigned n>
379 template <
unsigned n,
typename S>
382 operator*(
const rgb<n>& lhs,
const mln::value::scalar_<S>& s);
384 template <
unsigned n,
typename S>
387 operator*(
const mln::value::scalar_<S>& s,
const rgb<n>& lhs);
392 template <
unsigned n,
typename S>
395 operator/(
const rgb<n>& lhs,
const mln::value::scalar_<S>& s);
408 # ifndef MLN_INCLUDE_ONLY
420 template <
unsigned n>
426 template <
unsigned n>
433 template <
unsigned n>
440 template <
unsigned n>
447 template <
unsigned n>
451 convert::from_to(v[0], this->v_[0]);
452 convert::from_to(v[1], this->v_[1]);
453 convert::from_to(v[2], this->v_[2]);
456 template <
unsigned n>
460 mln_precondition(r >= 0);
461 mln_precondition(g >= 0);
462 mln_precondition(b >= 0);
463 mln_precondition(
unsigned(r) <= mln_max(
int_u<n>));
464 mln_precondition(
unsigned(g) <= mln_max(
int_u<n>));
465 mln_precondition(
unsigned(b) <= mln_max(
int_u<n>));
471 template <
unsigned n>
480 template <
unsigned n>
489 template <
unsigned n>
493 convert::from_to(mln_max(
int_u<n>) * 0.75, this->v_[0]);
494 convert::from_to(mln_max(
int_u<n>) * 0.75, this->v_[1]);
495 convert::from_to(mln_max(
int_u<n>) * 0.75, this->v_[2]);
498 template <
unsigned n>
502 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[0]);
503 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[1]);
504 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[2]);
507 template <
unsigned n>
511 convert::from_to(mln_max(int_u<n>) * 0.25, this->v_[0]);
512 convert::from_to(mln_max(int_u<n>) * 0.25, this->v_[1]);
513 convert::from_to(mln_max(int_u<n>) * 0.25, this->v_[2]);
516 template <
unsigned n>
520 this->v_[0] = mln_max(int_u<n>);
525 template <
unsigned n>
530 this->v_[1] = mln_max(int_u<n>);
534 template <
unsigned n>
540 this->v_[2] = mln_max(int_u<n>);
543 template <
unsigned n>
547 convert::from_to(mln_max(int_u<n>) * 0.75, this->v_[0]);
548 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[1]);
549 convert::from_to(mln_max(int_u<n>) * 0.25, this->v_[2]);
552 template <
unsigned n>
556 convert::from_to(mln_max(int_u<n>) * 0.75, this->v_[0]);
557 this->v_[1] = mln_max(int_u<n>);
561 template <
unsigned n>
565 this->v_[0] = mln_max(int_u<n>);
566 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[1]);
570 template <
unsigned n>
574 this->v_[0] = mln_max(int_u<n>);
575 convert::from_to(mln_max(int_u<n>) * 0.75, this->v_[1]);
576 convert::from_to(mln_max(int_u<n>) * 0.75, this->v_[2]);
579 template <
unsigned n>
583 convert::from_to(mln_max(int_u<n>) * 0.75, this->v_[0]);
585 convert::from_to(mln_max(int_u<n>) * 0.25, this->v_[2]);
588 template <
unsigned n>
593 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[1]);
594 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[2]);
597 template <
unsigned n>
601 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[0]);
603 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[2]);
606 template <
unsigned n>
611 this->v_[1] = mln_max(int_u<n>);
612 this->v_[2] = mln_max(int_u<n>);
615 template <
unsigned n>
619 this->v_[0] = mln_max(int_u<n>);
621 this->v_[2] = mln_max(int_u<n>);
624 template <
unsigned n>
628 this->v_[0] = mln_max(int_u<n>);
629 this->v_[1] = mln_max(int_u<n>);
633 template <
unsigned n>
637 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[0]);
638 convert::from_to(mln_max(int_u<n>) * 0.50, this->v_[1]);
642 template <
unsigned n>
653 template <
unsigned n>
660 template <
unsigned n>
669 template <
unsigned n>
678 template <
unsigned n>
680 typename rgb<n>::interop
681 operator+(
const typename rgb<n>::interop& lhs,
const rgb<n>& rhs)
683 typename rgb<n>::interop tmp(lhs + rhs.to_interop());
687 template <
unsigned n>
689 typename rgb<n>::interop
696 template <
unsigned n>
705 template <
unsigned n>
707 typename rgb<n>::interop
708 operator-(
const typename rgb<n>::interop& lhs,
const rgb<n>& rhs)
710 typename rgb<n>::interop tmp(lhs - rhs.to_interop());
714 template <
unsigned n,
typename S>
716 typename rgb<n>::interop
717 operator*(
const rgb<n>& lhs,
const mln::value::scalar_<S>& s)
723 template <
unsigned n,
typename S>
726 operator*(
const mln::value::scalar_<S>& s,
const rgb<n>& lhs)
732 template <
unsigned n,
typename S>
734 typename rgb<n>::interop
735 operator/(
const rgb<n>& lhs,
const mln::value::scalar_<S>& s)
741 template <
unsigned n>
743 std::ostream& operator<<(std::ostream& ostr, const rgb<n>& v)
751 template <
unsigned n>
753 std::istream& operator>>(std::istream& istr,
rgb<n>& c)
755 return istr >> c.
red() >> c.green() >> c.blue();
768 template <
typename T,
unsigned m>
771 from_to_(
const algebra::vec<3,T>& from, value::rgb<m>&
to)
773 algebra::vec<3, unsigned> tmp;
774 for (
unsigned i = 0; i < 3; ++i)
775 tmp[i] = static_cast<unsigned>(from[i]);
777 to = value::rgb<m>(tmp);
781 template <
unsigned m>
783 from_to_(
bool from, value::rgb<m>& to)
785 static literal::white_t* white_ = 0;
786 static literal::black_t* black_ = 0;
796 template <
unsigned m>
798 from_to_(
const value::int_u<m>& from, value::rgb<m>& to)
800 to = value::rgb<m>(from, from, from);
803 template <
typename H,
typename S,
typename L>
805 from_to_(
const value::hsl_<H,S,L>& from, value::rgb<8>& to)
817 template <
unsigned m>
819 from_to_(
const value::rgb<m>& from,
bool& to)
821 to = (from.red() != 0 && from.green() != 0 && from.blue() != 0);
830 # endif // ! MLN_INCLUDE_ONLY
833 #endif // ! MLN_VALUE_RGB_HH