Subversion Repositories DIN Is Noise

Rev

Rev 2009 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1528 jag 1
/*
2
* color.h
2097 jag 3
* DIN Is Noise is copyright (c) 2006-2024 Jagannathan Sampath
1713 jag 4
* DIN Is Noise is released under GNU Public License 2.0
1528 jag 5
* For more information, please visit https://dinisnoise.org/
6
*/
7
 
8
 
9
#ifndef __color__
10
#define __color__
11
 
12
#include "dingl.h"
13
#include "random.h"
14
#include "utils.h"
1897 jag 15
#include <fstream>
1528 jag 16
 
17
inline void hex2rgb (unsigned char hr, unsigned char hg, unsigned char hb, float& r, float& g, float& b) {
18
  const unsigned char ff = 0xff;
19
  const float ff1 = 1.0f / ff;
20
  r = hr * ff1;
21
  g = hg * ff1;
22
  b = hb * ff1;
23
}
24
 
25
struct color {
26
  float r, g, b;
27
  color (float rr = 0, float gg = 0, float bb = 0) {
28
    r = rr;
29
    g = gg;
30
    b = bb;
31
  }
32
  color (const color& t, const color& s) {
33
    r = t.r - s.r;
34
    g = t.g - s.g;
35
    b = t.b - s.b;
36
  }
37
  color& operator*= (float v) {
38
    r *= v;
39
    g *= v;
40
    b *= v;
41
    return *this;
42
  }
43
};
44
 
45
inline void set_rnd_color (float& r, float& g, float& b, float m0 = 0, float m1 = 1) {
46
  rnd<float> rd (m0, m1);
47
  r = rd(); g = rd (); b = rd ();
48
}
49
 
50
inline void set_color (unsigned char hr, unsigned char hg, unsigned char hb, float a = 1) {
51
  float r, g, b;
52
  hex2rgb (hr, hg, hb, r, g, b);
53
  glColor4f (r, g, b, a);
54
}
55
 
56
inline void blend_color (const color& source, const color& target, color& result, float amount) {
57
  color delta (target, source);
58
  delta *= amount;
59
  result.r = source.r + delta.r;
60
  result.g = source.g + delta.g;
61
  result.b = source.b + delta.b;
62
}
63
 
64
struct color_data_t {
65
  color clr[2];
66
  color out;
67
  int i;
68
  float p;
69
};
70
 
71
struct get_color {
72
  static color_data_t data;
73
  static void update_data ();
74
  virtual color& operator()() = 0;
75
};
76
 
77
struct get_fixed_color : get_color {
78
  int ii;
79
  get_fixed_color (int _i) { ii = _i;}
80
  color& operator() () {return data.clr [ii];}
81
};
82
 
83
struct get_random_color : get_color {
84
  color& operator() () {
85
    color& c0 = data.clr[0];
86
    color& c1 = data.clr[1];
87
    rnd<float> r(c0.r, c1.r), g(c0.g, c1.g), b(c0.b, c1.b);
88
    data.out.r = r(); data.out.g = g(); data.out.b = b();
89
    return data.out;
90
  }
91
};
92
 
93
struct get_blend_color : get_color {
94
  color& operator() () {
95
    color& c0 = data.clr[0];
96
    color& c1 = data.clr[1];
97
    blend_color (c0, c1, data.out, data.p);
98
    return data.out;
99
  }
100
};
101
 
102
 
103
struct colorer_t {
104
  enum {TOP, BOTTOM, BLEND, RANDOM}; 
105
  static const char* s_schemes [];
1924 jag 106
  #define maxs_ 4
107
  #define maxs__1 (maxs_ - 1)
1528 jag 108
  int i;
1924 jag 109
  get_color* schemes[maxs_];
1528 jag 110
  color& operator() () {
111
    get_color& gci = *schemes[i];
112
    return gci ();
113
  }
114
  colorer_t& operator+= (int ds) {
115
    i += ds;
1924 jag 116
    wrap (0, i, maxs__1);
1528 jag 117
    return *this;
118
  }
119
  const char* get_scheme_name () {
120
    return s_schemes[i];
121
  }
122
};
123
 
1897 jag 124
std::ostream& operator<< (std::ostream&, colorer_t&);
125
std::istream& operator>> (std::istream&, colorer_t&);
126
 
127
 
1528 jag 128
#endif