ROOT logo
// $Id: ZColor.h 2359 2010-03-24 20:29:39Z matevz $

// Copyright (C) 1999-2008, Matevz Tadel. All rights reserved.
// This file is part of GLED, released under GNU General Public License version 2.
// For the licensing terms see $GLEDSYS/LICENSE or http://www.gnu.org/.

#ifndef GledCore_ZColor_H
#define GledCore_ZColor_H

#include <Gled/GledTypes.h>

class ZColor
{
protected:
  Float_t mC[4];

public:
  ZColor()             { mC[0] = 1; mC[1] = 1; mC[2] = 1; mC[3] = 1;}
  ZColor(Float_t gray) { mC[0] = mC[1] = mC[2] = gray; mC[3]=1; }
  ZColor(Float_t r, Float_t g, Float_t b, Float_t a = 1)
  { mC[0]=r; mC[1]=g; mC[2]=b; mC[3]=a; }
  virtual ~ZColor() {}

  Float_t r() const { return mC[0]; }
  Float_t g() const { return mC[1]; }
  Float_t b() const { return mC[2]; }
  Float_t a() const { return mC[3]; }
  Float_t gray() const { return (mC[0]+mC[1]+mC[2])/3; }

  void r(Float_t x) { mC[0] = x; }
  void g(Float_t x) { mC[1] = x; }
  void b(Float_t x) { mC[2] = x; }
  void a(Float_t x) { mC[3] = x; }
  void rgba(Float_t r, Float_t g, Float_t b, Float_t a = 1)
  { mC[0]=r; mC[1]=g; mC[2]=b; mC[3]=a; }
  void gray(Float_t gray, Float_t a = 1)
  { mC[0]=mC[1]=mC[2]=gray; mC[3]=a; }

  void rgba_from_ubyte(UChar_t r, UChar_t g, UChar_t b, UChar_t a = 255)
  { Float_t f=1.0f/255; mC[0]=f*r; mC[1]=f*g; mC[2]=f*b; mC[3]=f*a; }
  void rgba_from_ubyte(UChar_t* c)
  { Float_t f=1.0f/255; mC[0]=f*c[0]; mC[1]=f*c[1]; mC[2]=f*c[2]; mC[3]=f*c[3]; }

  void to_ubyte(UChar_t* ub, bool wrap=false) const;
  void rgb_to_ubyte(UChar_t* ub, bool wrap=false) const;

  const Float_t* operator()() const { return mC; }
  const Float_t* array()      const { return mC; }
  Float_t  operator[](int i) const { return mC[i]; }
  Float_t& operator[](int i) { return mC[i]; }

  ZColor& operator*=(Float_t a)
  {
    mC[0]*=a; mC[1]*=a; mC[2]*=a; mC[3]*=a;
    return *this;
  }

  ZColor& operator+=(ZColor& x)
  {
    mC[0] += x[0]; mC[1] += x[1]; mC[2] += x[2]; mC[3] += x[3];
    return *this;
  }

  friend ZColor operator*(Float_t a, const ZColor& x);
  friend ZColor operator+(const ZColor& x, const ZColor& y);
  friend ZColor operator-(const ZColor& x, const ZColor& y);
  friend ZColor operator/(const ZColor& x, const ZColor& y);

  friend ostream& operator<<(ostream& s, const ZColor& c);

  ClassDefNV(ZColor,1); // Color as RGBA float quadruple.
};

ostream& operator<<(ostream& s, const ZColor& c);


//------------------------------------------------------------------------------
// Inlines
//------------------------------------------------------------------------------

inline void ZColor::to_ubyte(UChar_t* ub, bool wrap) const
{
  if (wrap)
  {
    for (int i=0; i<4; ++i)
      ub[i] = (UChar_t)(255*mC[i]);
  }
  else
  {
    for (int i=0; i<4; ++i)
      ub[i] = mC[i] >= 1 ? 255 : (mC[i] <= 0 ? 0 : (UChar_t)(255*mC[i]));
  }
}

inline void ZColor::rgb_to_ubyte(UChar_t* ub, bool wrap) const
{
  if (wrap)
  {
    for (int i=0; i<3; ++i)
      ub[i] = (UChar_t)(255*mC[i]);
  }
  else
  {
    for (int i=0; i<3; ++i)
      ub[i] = mC[i] >= 1 ? 255 : (mC[i] <= 0 ? 0 : (UChar_t)(255*mC[i]));
  }
}


//------------------------------------------------------------------------------
// Inline operators
//------------------------------------------------------------------------------

inline ZColor operator*(float a, const ZColor& x)
{
  // Scale color, alpha inchanged.
  return ZColor(a*x.mC[0], a*x.mC[1], a*x.mC[2], x.mC[3]);
}

inline ZColor operator+(const ZColor& x, const ZColor& y)
{
  // Add colors, alpha = 1.
  return ZColor(x.mC[0]+y.mC[0], x.mC[1]+y.mC[1], x.mC[2]+y.mC[2]);
}

inline ZColor operator-(const ZColor& x, const ZColor& y)
{
  // Subtract colors, alpha = 1.
  return ZColor(x.mC[0]-y.mC[0], x.mC[1]-y.mC[1], x.mC[2]-y.mC[2]);
}

inline ZColor operator/(const ZColor& x, const ZColor& y)
{
  // Average color, also alpha.
  return ZColor(0.5f*(x.mC[0]+y.mC[0]), 0.5f*(x.mC[1]+y.mC[1]),
		0.5f*(x.mC[2]+y.mC[2]), 0.5f*(x.mC[3]+y.mC[3]));
}

#endif
 ZColor.h:1
 ZColor.h:2
 ZColor.h:3
 ZColor.h:4
 ZColor.h:5
 ZColor.h:6
 ZColor.h:7
 ZColor.h:8
 ZColor.h:9
 ZColor.h:10
 ZColor.h:11
 ZColor.h:12
 ZColor.h:13
 ZColor.h:14
 ZColor.h:15
 ZColor.h:16
 ZColor.h:17
 ZColor.h:18
 ZColor.h:19
 ZColor.h:20
 ZColor.h:21
 ZColor.h:22
 ZColor.h:23
 ZColor.h:24
 ZColor.h:25
 ZColor.h:26
 ZColor.h:27
 ZColor.h:28
 ZColor.h:29
 ZColor.h:30
 ZColor.h:31
 ZColor.h:32
 ZColor.h:33
 ZColor.h:34
 ZColor.h:35
 ZColor.h:36
 ZColor.h:37
 ZColor.h:38
 ZColor.h:39
 ZColor.h:40
 ZColor.h:41
 ZColor.h:42
 ZColor.h:43
 ZColor.h:44
 ZColor.h:45
 ZColor.h:46
 ZColor.h:47
 ZColor.h:48
 ZColor.h:49
 ZColor.h:50
 ZColor.h:51
 ZColor.h:52
 ZColor.h:53
 ZColor.h:54
 ZColor.h:55
 ZColor.h:56
 ZColor.h:57
 ZColor.h:58
 ZColor.h:59
 ZColor.h:60
 ZColor.h:61
 ZColor.h:62
 ZColor.h:63
 ZColor.h:64
 ZColor.h:65
 ZColor.h:66
 ZColor.h:67
 ZColor.h:68
 ZColor.h:69
 ZColor.h:70
 ZColor.h:71
 ZColor.h:72
 ZColor.h:73
 ZColor.h:74
 ZColor.h:75
 ZColor.h:76
 ZColor.h:77
 ZColor.h:78
 ZColor.h:79
 ZColor.h:80
 ZColor.h:81
 ZColor.h:82
 ZColor.h:83
 ZColor.h:84
 ZColor.h:85
 ZColor.h:86
 ZColor.h:87
 ZColor.h:88
 ZColor.h:89
 ZColor.h:90
 ZColor.h:91
 ZColor.h:92
 ZColor.h:93
 ZColor.h:94
 ZColor.h:95
 ZColor.h:96
 ZColor.h:97
 ZColor.h:98
 ZColor.h:99
 ZColor.h:100
 ZColor.h:101
 ZColor.h:102
 ZColor.h:103
 ZColor.h:104
 ZColor.h:105
 ZColor.h:106
 ZColor.h:107
 ZColor.h:108
 ZColor.h:109
 ZColor.h:110
 ZColor.h:111
 ZColor.h:112
 ZColor.h:113
 ZColor.h:114
 ZColor.h:115
 ZColor.h:116
 ZColor.h:117
 ZColor.h:118
 ZColor.h:119
 ZColor.h:120
 ZColor.h:121
 ZColor.h:122
 ZColor.h:123
 ZColor.h:124
 ZColor.h:125
 ZColor.h:126
 ZColor.h:127
 ZColor.h:128
 ZColor.h:129
 ZColor.h:130
 ZColor.h:131
 ZColor.h:132
 ZColor.h:133
 ZColor.h:134
 ZColor.h:135
 ZColor.h:136
 ZColor.h:137
 ZColor.h:138
 ZColor.h:139