NCBI C++ ToolKit
glcolortable.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_OPENGL___GL_COLOR_TABLE__HPP
2 #define GUI_OPENGL___GL_COLOR_TABLE__HPP
3 
4 /* $Id: glcolortable.hpp 42537 2019-03-19 19:27:54Z katargir $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Mike DiCuccio, Andrey Yazhuk
30  *
31  * File Description:
32  * Incremental and mutually contrastive color table generator.
33  */
34 
35 
36 #include <corelib/ncbiobj.hpp>
37 
38 #include <gui/opengl.h>
39 #include <gui/utils/rgba_color.hpp>
41 
42 #include <algorithm>
43 #include <math.h>
44 
45 /** @addtogroup GUI_OPENGL
46  *
47  * @{
48  */
49 
50 
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// CRgbaGradColorTable
55 /// Provides a storage for colors (to eliminate color creation overhead) and
56 /// Functions for initializing the table.
58 {
59 public:
63 
65 
67 
68  size_t GetSize() const;
69  void SetSize(size_t size);
70 
71  inline CRgbaColor& GetColor(size_t i);
72  inline const CRgbaColor& GetColor(size_t i) const;
73 
74  inline CRgbaColor& operator[](int i);
75  inline const CRgbaColor& operator[](int i) const;
76 
77  /// On exact match, returns true with index in idx, false otherwise
78  bool FindColor(const CRgbaColor& c, size_t& idx) const;
79 
80  /// Return index of color with minimum color disance (as defined in CRgbaColor) from c.
81  /// If the table is empty the return value will be >=2 as sqrt(3.0) is max color distance.
82  float FindClosestColor(const CRgbaColor& c, size_t& idx) const;
83 
84  /// returns color index by the normalized value; normalized values is a value
85  /// in the [0.0, 1.0] range that is mapped to the index range of color table [0, size-1]
86  inline int GetIndex(double norm) const;
87 
88  /// Empty color table.
89  void ClearColors();
90 
91  /// Add a single color to the table
92  size_t AddColor(const CRgbaColor& c);
93 
94  /// initialize the whole table with gradient colors in [start_c, end_c] range
95  void FillGradient(const CRgbaColor& start_c, const CRgbaColor& end_c);
96 
97  /// initialize part of the table specified by the start index "start_i" and
98  /// the number of element "n" with gradient colors in [start_c, end_c] range
99  void FillGradient(int start_i, int n,
100  const CRgbaColor& start_c,
101  const CRgbaColor& end_c);
102 
103  /// initialize part of the table specified by the start index "start_i" and
104  /// the number of element "n" with gradient colors. First half of the table
105  /// is initialized with [start_c, mid_c], the second part with [mid_c, end_c]
106  void FillGradient(int start_i, int n,
107  const CRgbaColor& start_c,
108  const CRgbaColor& mid_c,
109  const CRgbaColor& end_c);
110 
111  ///
112  /// OpenGL parameters and operations to allow color table to (also) be stored
113  /// as a 1D texture.
114  ///
115 
116  /// Create a 1D texture from the current table and make alpha_levels copies of
117  /// the data so that user can access the same color at varying alpha levels
118  void LoadTexture(int alpha_levels = 8);
119 
120  /// Return object holding the 1D texture
121  I3DTexture* GetTexture() { return m_Texture; }
122 
123  /// Get position for color with alpha level closest to 'alpha'
124  float GetColorTexCoord(short idx, float alpha) const;
125  /// Get color given a texture coord
126  CRgbaColor GetTexCoordColor(float f) const;
127 
128 protected:
129  vector<CRgbaColor> m_Colors;
130 
131  // Support for (optionally) putting the colors into a 1-dimensional
132  // OpenGL texture
133 
134  // the texture id
136 
137  /// Width of the 1D texture (if created). May be larger than
138  /// m_Colors.size() since it must be a power of 2.
139  GLsizei m_TextureWidth;
140 
141  /// Number of alpha levels stored in the texture
143 
144  /// Maximum size of color texture. Getting close to or over this number
145  /// will reduce color pallette and available alpha levels.
147 };
148 
149 ///////////////////////////////////////////////////////////////////////////////
150 // inlines
151 
153 {
154  _ASSERT(i < m_Colors.size());
155  return m_Colors[i];
156 }
157 
158 inline const CRgbaColor& CRgbaGradColorTable::GetColor(size_t i) const
159 {
160  _ASSERT(i < m_Colors.size());
161  return m_Colors[i];
162 }
163 
164 
166 {
167  return GetColor(i);
168 }
169 
170 
172 {
173  return GetColor(i);
174 }
175 
176 
177 inline int CRgbaGradColorTable::GetIndex(double norm) const
178 {
179  int index = (int) floor(norm * m_Colors.size());
180  index = std::min(index, (int) m_Colors.size() - 1);
181  return index;
182 }
183 
185 
186 /* @} */
187 
188 #endif // GUI_OPENGL___GL_COLOR_TABLE__HPP
CObject –.
Definition: ncbiobj.hpp:180
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
CRgbaGradColorTable Provides a storage for colors (to eliminate color creation overhead) and Function...
CRgbaColor & GetColor(CSeqFeatData::ESubtype subtype)
GLsizei m_TextureWidth
Width of the 1D texture (if created).
vector< CRgbaColor > m_Colors
int m_TextureAlphaLevels
Number of alpha levels stored in the texture.
GLint m_MaxColorTableSize
Maximum size of color texture.
int GetIndex(double norm) const
returns color index by the normalized value; normalized values is a value in the [0....
CIRef< I3DTexture > m_Texture
CRgbaColor & operator[](int i)
I3DTexture * GetTexture()
Return object holding the 1D texture.
CRgbaColor & GetColor(size_t i)
CObject & operator=(const CObject &src) THROWS_NONE
Assignment operator.
Definition: ncbiobj.hpp:482
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_GUIOPENGL_EXPORT
Definition: gui_export.h:514
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
yy_size_t n
const struct ncbi::grid::netcache::search::fields::SIZE size
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
T min(T x_, T y_)
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
Standard mechanism to include OpenGL headers for all platforms.
#define _ASSERT
Modified on Sun Apr 21 03:43:25 2024 by modify_doxy.py rev. 669887