Canola  0.8.D001
lib/icon_pixel.cc
Go to the documentation of this file.
00001 //
00002 // canola - canon canola 1614p emulator
00003 // Copyright (C) 2011 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, version 3, as
00007 // published by the Free Software Foundation.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License along
00015 // with this program. If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 #include <lib/icon_pixel.h>
00019 #include <lib/std_string_printf.h>
00020 
00021 
00022 const icon_pixel icon_pixel::black(0., 0., 0., 1.);
00023 const icon_pixel icon_pixel::white(1., 1., 1., 1.);
00024 const icon_pixel icon_pixel::transparent;
00025 
00026 
00027 std::string
00028 icon_pixel::representation(void)
00029     const
00030 {
00031     return
00032         std_string_printf
00033         (
00034             "{ r=%g, g=%g, b=%g, a=%g }",
00035             get_red(),
00036             get_green(),
00037             get_blue(),
00038             get_alpha()
00039         );
00040 }
00041 
00042 
00043 bool
00044 icon_pixel::operator==(const icon_pixel &rhs)
00045     const
00046 {
00047     if (get_alpha_short() == 0 && rhs.get_alpha_short() == 0)
00048         return true;
00049     return
00050         (
00051             get_red_short() == rhs.get_red_short()
00052         &&
00053             get_green_short() == rhs.get_green_short()
00054         &&
00055             get_blue_short() == rhs.get_blue_short()
00056         &&
00057             get_alpha_short() == rhs.get_alpha_short()
00058         );
00059 }
00060 
00061 
00062 void
00063 icon_pixel::operator*=(const icon_pixel &rhs)
00064 {
00065     if (is_transparent())
00066         return;
00067     if (!real_valid)
00068         convert_short_to_real();
00069     if (rhs.is_transparent())
00070     {
00071         *this = transparent;
00072         return;
00073     }
00074     short_valid = false;
00075     red_real *= rhs.get_red();
00076     green_real *= rhs.get_green();
00077     blue_real *= rhs.get_blue();
00078     alpha_real *= rhs.get_alpha();
00079 }
00080 
00081 
00082 icon_pixel
00083 icon_pixel::operator*(const icon_pixel &rhs)
00084     const
00085 {
00086     icon_pixel result(*this);
00087     result *= rhs;
00088     return result;
00089 }
00090 
00091 
00092 void
00093 icon_pixel::operator*=(double rhs)
00094 {
00095     //
00096     // The alpha channel is deliberately left unchanged.
00097     // This operates on the intensity only.
00098     //
00099     if (is_transparent())
00100         return;
00101     if (!real_valid)
00102         convert_short_to_real();
00103     short_valid = false;
00104 
00105     red_real *= rhs;
00106     if (red_real < 0)
00107         red_real = 0.;
00108     else if (red_real > 1.)
00109         red_real = 1.;
00110 
00111     green_real *= rhs;
00112     if (green_real < 0)
00113         green_real = 0.;
00114     else if (green_real > 1.)
00115         green_real = 1.;
00116 
00117     blue_real *= rhs;
00118     if (blue_real < 0)
00119         blue_real = 0.;
00120     else if (blue_real > 1.)
00121         blue_real = 1.;
00122 }
00123 
00124 
00125 icon_pixel
00126 icon_pixel::operator*(double rhs)
00127     const
00128 {
00129     icon_pixel result(*this);
00130     result *= rhs;
00131     return result;
00132 }
00133 
00134 
00135 icon_pixel::real_type
00136 icon_pixel::get_intensity()
00137     const
00138 {
00139     if (!real_valid)
00140         convert_short_to_real();
00141     return (0.299 * red_real + 0.587 * green_real + 0.114 * blue_real);
00142 }
00143 
00144 
00145 icon_pixel
00146 icon_pixel::monochrome()
00147     const
00148 {
00149     real_type y = get_intensity();
00150     return icon_pixel(y, y, y, alpha_real);
00151 }