reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofColor.h
Go to the documentation of this file.
1#pragma once
2
3#define GLM_FORCE_CTOR_INIT
4#include "glm/gtx/wrap.hpp"
5#include <typeinfo>
6#include <iostream>
7#include <limits>
8
19template<typename PixelType>
21public:
24
27 r(limit()),
28 g(limit()),
29 b(limit()),
30 a(limit()) {};
31
38 ofColor_(float red, float green, float blue, float alpha = limit());
39
44 ofColor_(float gray, float alpha = limit());
45
53 template<typename SrcType>
55
60 ofColor_(const ofColor_<PixelType>& color, float alpha);
61
68 static ofColor_<PixelType> fromHsb(float hue,
69 float saturation,
70 float brightness,
71 float alpha = limit());
72
77 static ofColor_<PixelType> fromHex(int hexColor, float alpha = limit());
78
80
83
87 union {
88 struct {
89 PixelType r;
90 PixelType g;
91 PixelType b;
92 PixelType a;
93 };
94 PixelType v[4];
95 };
96
98
101
108 void set(float red, float green, float blue, float alpha = limit());
109
114 void set(float gray, float alpha = limit());
115
121 void set(const ofColor_<PixelType>& color);
122
130 void setHex(int hexColor, float alpha = limit());
131
135 void setHue(float hue);
136
140 void setHueAngle(float angle);
141
148 void setSaturation(float saturation);
149
153 void setBrightness(float brightness);
154
161 void setHsb(float hue,
162 float saturation,
163 float brightness,
164 float alpha = limit());
165
167
170
178
187
198
209 ofColor_<PixelType>& lerp(const ofColor_<PixelType>& target, float amount);
210
212
215
221
227
233
234
244 float amount) const;
245
251 int getHex() const;
252
260 float getHue() const;
261
269 float getHueAngle() const;
270
278 float getSaturation() const;
279
287 float getBrightness() const;
288
295 float getLightness() const;
296
305 void getHsb(float& hue, float& saturation, float& brightness) const;
306
310 static float limit();
311
313
316
328 template<typename SrcType>
330
335 ofColor_<PixelType>& operator = (float value);
336
340 bool operator == (const ofColor_<PixelType>& color) const;
341
344 bool operator != (const ofColor_<PixelType>& color) const;
345
355
364 ofColor_<PixelType> operator + (float color) const;
365
375
384 ofColor_<PixelType>& operator += (float color);
385
396
405 ofColor_<PixelType> operator - (float value) const;
406
414
423 ofColor_<PixelType>& operator -= (float color);
424
435
444 ofColor_<PixelType> operator * (float value) const;
445
456
465 ofColor_<PixelType>& operator *= (float value);
466
476
485 ofColor_<PixelType> operator / (float value) const;
486
494
503 ofColor_<PixelType>& operator /= (float value);
504
512 const PixelType& operator [] (std::size_t n) const;
513
521 PixelType& operator [] (std::size_t n);
522
531 friend std::ostream& operator << (std::ostream& os, const ofColor_<PixelType>& color) {
532 if(sizeof(PixelType) == 1) {
533 os << (int) color.r << ", " << (int) color.g << ", " << (int) color.b << ", " << (int) color.a;
534 } else {
535 os << color.r << ", " << color.g << ", " << color.b << ", " << color.a;
536 }
537 return os;
538 }
539
548 friend std::istream& operator >> (std::istream& is, ofColor_<PixelType>& color) {
549 if(sizeof(PixelType) == 1) {
550 int component;
551 is >> std::skipws >> component;
552 color.r = component;
553 is.ignore(1);
554 is >> std::skipws >> component;
555 color.g = component;
556 is.ignore(1);
557 is >> std::skipws >> component;
558 color.b = component;
559 is.ignore(1);
560 is >> std::skipws >> component;
561 color.a = component;
562 }else{
563 is >> std::skipws >> color.r;
564 is.ignore(1);
565 is >> std::skipws >> color.g;
566 is.ignore(1);
567 is >> std::skipws >> color.b;
568 is.ignore(1);
569 is >> std::skipws >> color.a;
570 }
571 return is;
572 }
573
574
576
593
613
614
616
617
618private:
619 template<typename SrcType>
620 void copyFrom(const ofColor_<SrcType>& mom);
621
622};
623
626
630
634
638
640
641
642template<typename PixelType>
643template<typename SrcType>
645 copyFrom(mom);
646}
647
648template<typename PixelType>
649template<typename SrcType>
651 copyFrom(mom);
652 return *this;
653}
654
655template<typename PixelType>
656template<typename SrcType>
658 const float srcMax = mom.limit();
659 const float dstMax = limit();
660 const float factor = dstMax / srcMax;
661
662 if(typeid(SrcType) == typeid(float) || typeid(SrcType) == typeid(double)) {
663 // coming from float we need a special case to clamp the values
664 for(int i = 0; i < 4; i++){
665 v[i] = glm::clamp(float(mom[i]), 0.f, 1.f) * factor;
666 }
667 } else{
668 // everything else is a straight scaling
669 for(int i = 0; i < 4; i++){
670 v[i] = mom[i] * factor;
671 }
672 }
673}
674
675template <typename PixelType>
677 return color * val;
678}
679
680template<typename PixelType>
682 return std::numeric_limits<PixelType>::max();
683}
684
685template<>
686inline float ofColor_<float>::limit() {
687 return 1.f;
688}
689
690template<>
692 return
693 ((0xff & (unsigned char) r) << 16) |
694 ((0xff & (unsigned char) g) << 8) |
695 ((0xff & (unsigned char) b));
696}
697
698template<typename PixelType>
699inline int ofColor_<PixelType>::getHex() const {
700 return ((ofColor) *this).getHex();
701}
702
703template<>
704inline void ofColor_<unsigned char>::setHex(int hexColor, float alpha){
705 r = (hexColor >> 16) & 0xff;
706 g = (hexColor >> 8) & 0xff;
707 b = (hexColor >> 0) & 0xff;
708 a = alpha;
709}
710
711template<typename PixelType>
712inline void ofColor_<PixelType>::setHex (int hexColor, float alpha){
713 ofColor c = ofColor::fromHex(hexColor);
714 *this = c;
715 a = alpha;
716}
717
718
719extern template class ofColor_<char>;
720extern template class ofColor_<unsigned char>;
721extern template class ofColor_<short>;
722extern template class ofColor_<unsigned short>;
723extern template class ofColor_<int>;
724extern template class ofColor_<unsigned int>;
725extern template class ofColor_<long>;
726extern template class ofColor_<unsigned long>;
727extern template class ofColor_<float>;
728extern template class ofColor_<double>;
ofColor represents a color in openFrameworks.
Definition ofColor.h:20
static const ofColor_< PixelType > snow
Definition ofColor.h:610
friend std::istream & operator>>(std::istream &is, ofColor_< PixelType > &color)
An input stream operator.
Definition ofColor.h:548
static const ofColor_< PixelType > cadetBlue
Definition ofColor.h:596
static const ofColor_< PixelType > darkGrey
Definition ofColor.h:597
static const ofColor_< PixelType > aquamarine
Definition ofColor.h:595
static const ofColor_< PixelType > slateGrey
Definition ofColor.h:610
static const ofColor_< PixelType > fuchsia
Definition ofColor.h:600
static const ofColor_< PixelType > thistle
Definition ofColor.h:611
static const ofColor_< PixelType > royalBlue
Definition ofColor.h:609
ofColor_()
Construct a default white color.
Definition ofColor.h:26
static const ofColor_< PixelType > turquoise
Definition ofColor.h:611
ofColor_< PixelType > & operator*=(const ofColor_< PixelType > &color)
Clamped multiplication operator.
Definition ofColor.cpp:577
static const ofColor_< PixelType > olive
Definition ofColor.h:607
static const ofColor_< PixelType > coral
Definition ofColor.h:596
const PixelType & operator[](std::size_t n) const
Array subscript operator.
Definition ofColor.cpp:629
static const ofColor_< PixelType > limeGreen
Definition ofColor.h:605
static const ofColor_< PixelType > papayaWhip
Definition ofColor.h:608
static const ofColor_< PixelType > bisque
Definition ofColor.h:595
static const ofColor_< PixelType > brown
Definition ofColor.h:596
static const ofColor_< PixelType > mediumTurquoise
Definition ofColor.h:606
static const ofColor_< PixelType > lightSlateGray
Definition ofColor.h:604
static const ofColor_< PixelType > springGreen
Definition ofColor.h:611
ofColor_< PixelType > & operator=(const ofColor_< SrcType > &color)
Assign a color using an existing color.
Definition ofColor.h:650
static const ofColor_< PixelType > darkViolet
Definition ofColor.h:599
float getLightness() const
Calculate the lightness of the R, G and B components.
Definition ofColor.cpp:347
static const ofColor_< PixelType > floralWhite
Definition ofColor.h:600
static const ofColor_< PixelType > mediumSlateBlue
Definition ofColor.h:606
static const ofColor_< PixelType > darkSalmon
Definition ofColor.h:598
static const ofColor_< PixelType > aliceBlue
Definition ofColor.h:595
static const ofColor_< PixelType > plum
Definition ofColor.h:609
static ofColor_< PixelType > fromHsb(float hue, float saturation, float brightness, float alpha=limit())
Create an ofColor_ from an HSB representation.
Definition ofColor.cpp:195
static const ofColor_< PixelType > lightGoldenRodYellow
Definition ofColor.h:603
void setHue(float hue)
Set the hue of this color.
Definition ofColor.cpp:389
static const ofColor_< PixelType > lightGray
Definition ofColor.h:603
static const ofColor_< PixelType > lavender
Definition ofColor.h:602
static const ofColor_< PixelType > sienna
Definition ofColor.h:610
static const ofColor_< PixelType > mediumBlue
Definition ofColor.h:605
static const ofColor_< PixelType > darkMagenta
Definition ofColor.h:598
static const ofColor_< PixelType > darkOrange
Definition ofColor.h:598
static const ofColor_< PixelType > hotPink
Definition ofColor.h:601
static const ofColor_< PixelType > peachPuff
Definition ofColor.h:609
PixelType v[4]
The pixel values as an array.
Definition ofColor.h:94
static const ofColor_< PixelType > oldLace
Definition ofColor.h:607
ofColor_< PixelType > & normalize()
Normalize the R, G and B components.
Definition ofColor.cpp:259
static const ofColor_< PixelType > azure
Definition ofColor.h:595
static const ofColor_< PixelType > peru
Definition ofColor.h:609
void setHsb(float hue, float saturation, float brightness, float alpha=limit())
Set the color using HSB components.
Definition ofColor.cpp:419
static const ofColor_< PixelType > seaGreen
Definition ofColor.h:610
static const ofColor_< PixelType > dimGrey
Definition ofColor.h:600
static const ofColor_< PixelType > gray
Definition ofColor.h:594
static const ofColor_< PixelType > dimGray
Definition ofColor.h:600
static const ofColor_< PixelType > violet
Definition ofColor.h:611
static const ofColor_< PixelType > skyBlue
Definition ofColor.h:610
friend std::ostream & operator<<(std::ostream &os, const ofColor_< PixelType > &color)
An output stream operator.
Definition ofColor.h:531
static const ofColor_< PixelType > honeyDew
Definition ofColor.h:601
static const ofColor_< PixelType > chartreuse
Definition ofColor.h:596
static const ofColor_< PixelType > mediumPurple
Definition ofColor.h:605
ofColor_< PixelType > operator*(const ofColor_< PixelType > &color) const
Clamped multiplication operator.
Definition ofColor.cpp:561
static const ofColor_< PixelType > orangeRed
Definition ofColor.h:608
static const ofColor_< PixelType > lemonChiffon
Definition ofColor.h:602
static const ofColor_< PixelType > lightBlue
Definition ofColor.h:602
static const ofColor_< PixelType > chocolate
Definition ofColor.h:596
bool operator==(const ofColor_< PixelType > &color) const
Test two colors for equality.
Definition ofColor.cpp:483
bool operator!=(const ofColor_< PixelType > &color) const
Test two colors for inequality.
Definition ofColor.cpp:489
static const ofColor_< PixelType > darkSlateBlue
Definition ofColor.h:599
static const ofColor_< PixelType > teal
Definition ofColor.h:611
static const ofColor_< PixelType > steelBlue
Definition ofColor.h:611
static const ofColor_< PixelType > white
Definition ofColor.h:594
static const ofColor_< PixelType > lightSteelBlue
Definition ofColor.h:604
static const ofColor_< PixelType > blanchedAlmond
Definition ofColor.h:595
void setHueAngle(float angle)
Set the hue angle of this color.
Definition ofColor.cpp:398
static const ofColor_< PixelType > salmon
Definition ofColor.h:609
static const ofColor_< PixelType > saddleBrown
Definition ofColor.h:609
ofColor_< PixelType > getNormalized() const
A non-destructive version of normalize().
Definition ofColor.cpp:295
static const ofColor_< PixelType > mediumVioletRed
Definition ofColor.h:606
float getHue() const
Get the Hue of this color.
Definition ofColor.cpp:312
static const ofColor_< PixelType > lime
Definition ofColor.h:605
float getHueAngle() const
Get the Hue angle of this color.
Definition ofColor.cpp:321
static const ofColor_< PixelType > lightGrey
Definition ofColor.h:603
static const ofColor_< PixelType > forestGreen
Definition ofColor.h:600
static const ofColor_< PixelType > purple
Definition ofColor.h:609
ofColor_< PixelType > operator+(const ofColor_< PixelType > &color) const
Clamped addition operator.
Definition ofColor.cpp:495
PixelType g
The green color component.
Definition ofColor.h:90
float getBrightness() const
Calculate the brightness of of the R, G and B components.
Definition ofColor.cpp:335
static const ofColor_< PixelType > darkOrchid
Definition ofColor.h:598
static const ofColor_< PixelType > oliveDrab
Definition ofColor.h:607
static const ofColor_< PixelType > wheat
Definition ofColor.h:611
float getSaturation() const
Get the Saturation of this color.
Definition ofColor.cpp:326
static const ofColor_< PixelType > lightSlateGrey
Definition ofColor.h:604
PixelType r
The red color component.
Definition ofColor.h:89
static const ofColor_< PixelType > lightSeaGreen
Definition ofColor.h:604
static const ofColor_< PixelType > darkGreen
Definition ofColor.h:597
ofColor_< PixelType > & lerp(const ofColor_< PixelType > &target, float amount)
A linear interpolation between all components of two colors.
Definition ofColor.cpp:267
static const ofColor_< PixelType > tomato
Definition ofColor.h:611
ofColor_< PixelType > operator/(const ofColor_< PixelType > &color) const
Clamped division operator.
Definition ofColor.cpp:595
static const ofColor_< PixelType > silver
Definition ofColor.h:610
ofColor_(const ofColor_< SrcType > &color)
Construct an ofColor_ from an existing ofColor_.
Definition ofColor.h:644
static const ofColor_< PixelType > greenYellow
Definition ofColor.h:601
static const ofColor_< PixelType > lavenderBlush
Definition ofColor.h:602
PixelType a
The alpha color component.
Definition ofColor.h:92
static const ofColor_< PixelType > deepPink
Definition ofColor.h:599
static const ofColor_< PixelType > lightYellow
Definition ofColor.h:604
static const ofColor_< PixelType > pink
Definition ofColor.h:609
static const ofColor_< PixelType > lightSkyBlue
Definition ofColor.h:604
static const ofColor_< PixelType > darkKhaki
Definition ofColor.h:597
static const ofColor_< PixelType > beige
Definition ofColor.h:595
static const ofColor_< PixelType > midnightBlue
Definition ofColor.h:607
static const ofColor_< PixelType > ivory
Definition ofColor.h:602
static const ofColor_< PixelType > yellowGreen
Definition ofColor.h:612
ofColor_< PixelType > & clamp()
Clamp values between 0 and the limit().
Definition ofColor.cpp:240
static const ofColor_< PixelType > yellow
Definition ofColor.h:595
static const ofColor_< PixelType > cornsilk
Definition ofColor.h:596
static const ofColor_< PixelType > navajoWhite
Definition ofColor.h:607
static const ofColor_< PixelType > linen
Definition ofColor.h:605
static ofColor_< PixelType > fromHex(int hexColor, float alpha=limit())
Create an ofColor_ from a hexadecimal value.
Definition ofColor.cpp:206
void setSaturation(float saturation)
Set the saturation of this color.
Definition ofColor.cpp:403
static const ofColor_< PixelType > indianRed
Definition ofColor.h:601
static const ofColor_< PixelType > mediumAquaMarine
Definition ofColor.h:605
static const ofColor_< PixelType > grey
Definition ofColor.h:601
static const ofColor_< PixelType > darkOliveGreen
Definition ofColor.h:598
ofColor_< PixelType > getInverted() const
A non-destructive version of invert().
Definition ofColor.cpp:287
static const ofColor_< PixelType > mintCream
Definition ofColor.h:607
static const ofColor_< PixelType > cyan
Definition ofColor.h:594
static const ofColor_< PixelType > indigo
Definition ofColor.h:601
static const ofColor_< PixelType > lawnGreen
Definition ofColor.h:602
static const ofColor_< PixelType > orchid
Definition ofColor.h:608
static const ofColor_< PixelType > darkGoldenRod
Definition ofColor.h:597
static const ofColor_< PixelType > blueSteel
Definition ofColor.h:611
static const ofColor_< PixelType > blueViolet
Definition ofColor.h:596
static const ofColor_< PixelType > mediumSeaGreen
Definition ofColor.h:606
static const ofColor_< PixelType > tan
Definition ofColor.h:611
static const ofColor_< PixelType > black
Definition ofColor.h:594
static const ofColor_< PixelType > aqua
Definition ofColor.h:595
static const ofColor_< PixelType > green
Definition ofColor.h:594
static const ofColor_< PixelType > darkRed
Definition ofColor.h:598
static const ofColor_< PixelType > deepSkyBlue
Definition ofColor.h:600
static const ofColor_< PixelType > powderBlue
Definition ofColor.h:609
static const ofColor_< PixelType > darkGray
Definition ofColor.h:597
static const ofColor_< PixelType > darkCyan
Definition ofColor.h:597
static const ofColor_< PixelType > lightSalmon
Definition ofColor.h:603
static const ofColor_< PixelType > gainsboro
Definition ofColor.h:601
ofColor_< PixelType > operator-(const ofColor_< PixelType > &color) const
Clamped subtraction operator.
Definition ofColor.cpp:528
static const ofColor_< PixelType > red
Definition ofColor.h:594
static const ofColor_< PixelType > antiqueWhite
Definition ofColor.h:595
static const ofColor_< PixelType > burlyWood
Definition ofColor.h:596
int getHex() const
Get a 24-bit hexadecimal representation of the RGB color.
Definition ofColor.h:699
ofColor_< PixelType > getLerped(const ofColor_< PixelType > &target, float amount) const
A non-destructive version of lerp().
Definition ofColor.cpp:303
static const ofColor_< PixelType > goldenRod
Definition ofColor.h:601
ofColor_< PixelType > & invert()
Invert the R, G and B components.
Definition ofColor.cpp:250
static const ofColor_< PixelType > ghostWhite
Definition ofColor.h:601
static const ofColor_< PixelType > paleVioletRed
Definition ofColor.h:608
static const ofColor_< PixelType > rosyBrown
Definition ofColor.h:609
ofColor_< PixelType > & operator+=(const ofColor_< PixelType > &color)
Clamped addition operator.
Definition ofColor.cpp:511
static const ofColor_< PixelType > khaki
Definition ofColor.h:602
static const ofColor_< PixelType > orange
Definition ofColor.h:608
static const ofColor_< PixelType > paleGoldenRod
Definition ofColor.h:608
static const ofColor_< PixelType > cornflowerBlue
Definition ofColor.h:596
static const ofColor_< PixelType > whiteSmoke
Definition ofColor.h:611
static const ofColor_< PixelType > moccasin
Definition ofColor.h:607
ofColor_< PixelType > & operator/=(const ofColor_< PixelType > &color)
Clamped division operator.
Definition ofColor.cpp:611
static const ofColor_< PixelType > blue
Definition ofColor.h:594
static const ofColor_< PixelType > slateGray
Definition ofColor.h:610
void setHex(int hexColor, float alpha=limit())
Set an ofColor_ from a hexadecimal representation.
Definition ofColor.h:712
static const ofColor_< PixelType > maroon
Definition ofColor.h:605
static const ofColor_< PixelType > crimson
Definition ofColor.h:597
static const ofColor_< PixelType > mistyRose
Definition ofColor.h:607
static const ofColor_< PixelType > seaShell
Definition ofColor.h:610
static const ofColor_< PixelType > lightCyan
Definition ofColor.h:603
static const ofColor_< PixelType > darkBlue
Definition ofColor.h:597
static const ofColor_< PixelType > lightPink
Definition ofColor.h:603
static const ofColor_< PixelType > mediumOrchid
Definition ofColor.h:605
ofColor_< PixelType > getClamped() const
A non-destructive version of clamp().
Definition ofColor.cpp:279
void getHsb(float &hue, float &saturation, float &brightness) const
Extract the hue, saturation and brightness (HSB) from this color.
Definition ofColor.cpp:352
static const ofColor_< PixelType > navy
Definition ofColor.h:607
ofColor_< PixelType > & operator-=(const ofColor_< PixelType > &color)
Clamped subtraction operator.
Definition ofColor.cpp:544
void setBrightness(float brightness)
Set the brightness of this color.
Definition ofColor.cpp:411
static const ofColor_< PixelType > gold
Definition ofColor.h:601
static const ofColor_< PixelType > mediumSpringGreen
Definition ofColor.h:606
static const ofColor_< PixelType > paleGreen
Definition ofColor.h:608
static const ofColor_< PixelType > lightGreen
Definition ofColor.h:603
static const ofColor_< PixelType > paleTurquoise
Definition ofColor.h:608
static const ofColor_< PixelType > darkSlateGrey
Definition ofColor.h:599
static float limit()
Get the maximum value of a color component.
Definition ofColor.h:681
static const ofColor_< PixelType > lightCoral
Definition ofColor.h:602
static const ofColor_< PixelType > dodgerBlue
Definition ofColor.h:600
static const ofColor_< PixelType > darkSlateGray
Definition ofColor.h:599
static const ofColor_< PixelType > darkSeaGreen
Definition ofColor.h:598
static const ofColor_< PixelType > fireBrick
Definition ofColor.h:600
PixelType b
The blue color component.
Definition ofColor.h:91
static const ofColor_< PixelType > darkTurquoise
Definition ofColor.h:599
static const ofColor_< PixelType > sandyBrown
Definition ofColor.h:610
static const ofColor_< PixelType > slateBlue
Definition ofColor.h:610
static const ofColor_< PixelType > magenta
Definition ofColor.h:594
ofColor_< unsigned char > ofColor
Definition ofColor.h:629
ofColor_< float > ofFloatColor
Definition ofColor.h:637
ofColor_< PixelType > operator*(float val, const ofColor_< PixelType > &color)
Definition ofColor.h:676
ofColor_< unsigned short > ofShortColor
Definition ofColor.h:633
#define a
#define c
#define b