rgba_color.cpp
来自「ncbi源码」· C++ 代码 · 共 1,222 行 · 第 1/3 页
CPP
1,222 行
/* * =========================================================================== * PRODUCTION $Log: rgba_color.cpp,v $ * PRODUCTION Revision 1000.2 2004/06/01 21:05:10 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.13 * PRODUCTION * =========================================================================== *//* $Id: rgba_color.cpp,v 1000.2 2004/06/01 21:05:10 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Mike DiCuccio, Peter Meric * * File Description: * CRgbaColor - colors in the RGB colorspace, including transparency */#include <ncbi_pch.hpp>#include <gui/utils/rgba_color.hpp>#include <util/static_map.hpp>BEGIN_NCBI_SCOPE// add a color to this colorCRgbaColor& CRgbaColor::operator+=(const CRgbaColor& c1){ m_Rgba[0] += c1.m_Rgba[0]; m_Rgba[1] += c1.m_Rgba[1]; m_Rgba[2] += c1.m_Rgba[2]; m_Rgba[3] += c1.m_Rgba[3]; return *this;}CRgbaColor operator+(const CRgbaColor& c1, const CRgbaColor& c2){ return CRgbaColor(c1.GetRed() + c2.GetRed(), c1.GetBlue() + c2.GetBlue(), c1.GetGreen() + c2.GetGreen(), c1.GetAlpha() + c2.GetAlpha());}// multiply this color by a scalarCRgbaColor& CRgbaColor::operator*=(float f){ m_Rgba[0] *= f; m_Rgba[1] *= f; m_Rgba[2] *= f; m_Rgba[3] *= f; return *this;}CRgbaColor operator*(const CRgbaColor& c1, float f){ return CRgbaColor(c1.GetRed() * f, c1.GetBlue() * f, c1.GetGreen() * f, c1.GetAlpha() * f);}bool operator==(const CRgbaColor& c1, const CRgbaColor& c2){ return (c1.GetRed() == c2.GetRed() && c1.GetGreen() == c2.GetGreen() && c1.GetBlue() == c2.GetBlue() && c1.GetAlpha() == c2.GetAlpha());}CRgbaColor::CRgbaColor(){ m_Rgba[0] = m_Rgba[1] = m_Rgba[2] = 0.0f; m_Rgba[3] = 1.0f;}CRgbaColor::CRgbaColor(const string& s){ ScanFrom(s);}CRgbaColor::CRgbaColor(const float* color, size_t size){ for (size_t i = 0; i < 4; ++i) { m_Rgba[i] = i < size ? color[i] : (i == 3 ? 1.0f : 0.0f); }}CRgbaColor::CRgbaColor(float r, float g, float b){ m_Rgba[0] = r; m_Rgba[1] = g; m_Rgba[2] = b; m_Rgba[3] = 1.0f;}CRgbaColor::CRgbaColor(float r, float g, float b, float a){ m_Rgba[0] = r; m_Rgba[1] = g; m_Rgba[2] = b; m_Rgba[3] = a;}CRgbaColor::CRgbaColor(unsigned char r, unsigned char g, unsigned char b){ m_Rgba[0] = (float(r) / 255.0f); m_Rgba[1] = (float(g) / 255.0f); m_Rgba[2] = (float(b) / 255.0f); m_Rgba[3] = 1.0f;}CRgbaColor::CRgbaColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a){ m_Rgba[0] = (float(r) / 255.0f); m_Rgba[1] = (float(g) / 255.0f); m_Rgba[2] = (float(b) / 255.0f); m_Rgba[3] = (float(a) / 255.0f);}CRgbaColor::~CRgbaColor(){}void CRgbaColor::Set(float r, float g, float b){ m_Rgba[0] = r; m_Rgba[1] = g; m_Rgba[2] = b; m_Rgba[3] = 1.0f;}void CRgbaColor::Set(float r, float g, float b, float a){ m_Rgba[0] = r; m_Rgba[1] = g; m_Rgba[2] = b; m_Rgba[3] = a;}void CRgbaColor::Set(unsigned char r, unsigned char g, unsigned char b){ m_Rgba[0] = (float(r) / 255.0f); m_Rgba[1] = (float(g) / 255.0f); m_Rgba[2] = (float(b) / 255.0f); m_Rgba[3] = 1.0f;}void CRgbaColor::Set(unsigned char r, unsigned char g, unsigned char b, unsigned char a){ m_Rgba[0] = (float(r) / 255.0f); m_Rgba[1] = (float(g) / 255.0f); m_Rgba[2] = (float(b) / 255.0f); m_Rgba[3] = (float(a) / 255.0f);}void CRgbaColor::SetRed(float r){ m_Rgba[0] = (r > 1.0f) ? 1.0f : (r < 0.0f) ? 0.0f : r;}void CRgbaColor::SetRed(unsigned char r){ m_Rgba[0] = float(r) / 255.0f;}void CRgbaColor::SetGreen(float g){ m_Rgba[1] = (g > 1.0f) ? 1.0f : (g < 0.0f) ? 0.0f : g;}void CRgbaColor::SetGreen(unsigned char g){ m_Rgba[1] = float(g) / 255.0f;}void CRgbaColor::SetBlue(float b){ m_Rgba[2] = (b > 1.0f) ? 1.0f : (b < 0.0f) ? 0.0f : b;}void CRgbaColor::SetBlue(unsigned char b){ m_Rgba[2] = float(b) / 255.0f;}void CRgbaColor::SetAlpha(float a){ m_Rgba[3] = (a > 1.0f) ? 1.0f : (a < 0.0f) ? 0.0f : a;}void CRgbaColor::SetAlpha(unsigned char a){ m_Rgba[3] = float(a) / 255.0f;}float CRgbaColor::GetRed(void) const{ return m_Rgba[0];}float CRgbaColor::GetGreen(void) const{ return m_Rgba[1];}float CRgbaColor::GetBlue(void) const{ return m_Rgba[2];}float CRgbaColor::GetAlpha(void) const{ return m_Rgba[3];}unsigned char CRgbaColor::GetRedUC(void) const{ return (unsigned char) (m_Rgba[0] * 255.0f);}unsigned char CRgbaColor::GetGreenUC(void) const{ return (unsigned char) (m_Rgba[1] * 255.0f);}unsigned char CRgbaColor::GetBlueUC(void) const{ return (unsigned char) (m_Rgba[2] * 255.0f);}unsigned char CRgbaColor::GetAlphaUC(void) const{ return (unsigned char) (m_Rgba[3] * 255.0f);}void CRgbaColor::PrintTo(CNcbiOstream& strm) const{ PrintTo(strm, true);}void CRgbaColor::PrintTo(CNcbiOstream& strm, bool printAlpha) const{ strm << ToString(printAlpha);}string CRgbaColor::ToString(bool printAlpha /* = true */) const{ CNcbiOstrstream oss; oss << static_cast<unsigned>(GetRedUC()) << " " << static_cast<unsigned>(GetGreenUC()) << " " << static_cast<unsigned>(GetBlueUC()); if (printAlpha) { oss << " " << static_cast<unsigned>(GetAlphaUC()); } return CNcbiOstrstreamToString(oss);}void CRgbaColor::ScanFrom(const string& s){ string myStr(s); // look up if this is the name of a color ("blue"). string myColorStr(ColorStrFromName(myStr)); if ( ! myColorStr.empty()) { myStr = myColorStr; } list<string> toks; NStr::Split(myStr, " ", toks); list<string>::const_iterator iter = toks.begin(); if (toks.size() >= 3) { try { SetRed( static_cast<unsigned char>(NStr::StringToUInt(*iter++))); SetGreen(static_cast<unsigned char>(NStr::StringToUInt(*iter++))); SetBlue( static_cast<unsigned char>(NStr::StringToUInt(*iter++))); if (toks.size() > 3) { SetAlpha(static_cast<unsigned char>(NStr::StringToUInt(*iter))); } else { SetAlpha(1.0f); } } catch (const CStringException& e) { NCBI_THROW2(CStringException, eConvert, "String cannot be converted to a color [" + e.GetMsg() + "]", 0); } } else { NCBI_THROW2(CStringException, eConvert, "String cannot be converted to a color", 0); }}const float* CRgbaColor::GetColorArray(void) const{ return m_Rgba;}CRgbaColor CRgbaColor::Interpolate(const CRgbaColor& color1, const CRgbaColor& color2, float alpha){ _ASSERT(alpha >= 0 && alpha <= 1); return (color1 * alpha + color2 * (1.0f - alpha));}void CRgbaColor::Lighten(float alpha){ m_Rgba[0] = (m_Rgba[0] * (1.0f - alpha) + 1.0f * alpha); m_Rgba[1] = (m_Rgba[1] * (1.0f - alpha) + 1.0f * alpha); m_Rgba[2] = (m_Rgba[2] * (1.0f - alpha) + 1.0f * alpha);}void CRgbaColor::Darken(float alpha){ m_Rgba[0] = (m_Rgba[0] * (1.0f - alpha) + 0.0f * alpha); m_Rgba[1] = (m_Rgba[1] * (1.0f - alpha) + 0.0f * alpha); m_Rgba[2] = (m_Rgba[2] * (1.0f - alpha) + 0.0f * alpha);}//// the actual color array - put here because of its length//typedef pair<const char*, const char*> TColorElem;static const TColorElem sc_ColorArray[] = { TColorElem("alice blue", "240 248 255"), TColorElem("aliceblue", "240 248 255"), TColorElem("antique white", "250 235 215"), TColorElem("antiquewhite", "250 235 215"), TColorElem("antiquewhite1", "255 239 219"), TColorElem("antiquewhite2", "238 223 204"), TColorElem("antiquewhite3", "205 192 176"), TColorElem("antiquewhite4", "139 131 120"), TColorElem("aquamarine", "127 255 212"), TColorElem("aquamarine1", "127 255 212"), TColorElem("aquamarine2", "118 238 198"), TColorElem("aquamarine3", "102 205 170"), TColorElem("aquamarine4", "69 139 116"), TColorElem("azure", "240 255 255"),
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?