Blame c++/freetype/src/common.h

b8976b
#ifndef COMMON_H
b8976b
#define COMMON_H
b8976b
b8976b
b8976b
#include <cmath></cmath>
b8976b
#include <cstdlib></cstdlib>
b8976b
#include <algorithm></algorithm>
b8976b
b8976b
#include <glibmm refptr.h=""></glibmm>
b8976b
b8976b
b8976b
typedef float ColorReal;
b8976b
typedef double Real;
b8976b
typedef int Int;
b8976b
typedef unsigned int UInt;
b8976b
typedef long long int LongInt;
b8976b
typedef unsigned long long int ULongInt;
b8976b
b8976b
using Glib::RefPtr;
b8976b
b8976b
b8976b
const Real real_precision = 1e-10;
b8976b
const Real real_precision_sqr = real_precision * real_precision;
b8976b
b8976b
b8976b
class Shared {
b8976b
private:
b8976b
	int ref_count;
b8976b
public:
b8976b
	Shared(): ref_count(1) { }
b8976b
	Shared(const Shared&): ref_count(1) { }
b8976b
	virtual ~Shared() { };
b8976b
	
b8976b
	inline Shared& operator=(const Shared&) { return *this; }
b8976b
	inline void reference() {
b8976b
		++ref_count;
b8976b
	}
b8976b
	inline void unreference() {
b8976b
		if (--ref_count <= 0)
b8976b
			delete this;
b8976b
	}
b8976b
};
b8976b
b8976b
b8976b
class Color {
b8976b
public:
b8976b
	typedef ColorReal Channel;
b8976b
	union {
b8976b
		struct { Channel channels[4]; };
b8976b
		struct { Channel r, g, b, a; };
b8976b
	};
b8976b
	
b8976b
	inline explicit Color(Channel r = 0.0, Channel g = 0.0, Channel b = 0.0, Channel a = 0.0):
b8976b
		r(r), g(g), b(b), a(a) { }
b8976b
	
b8976b
	inline Color operator+ (const Color &other) const
b8976b
		{ return Color(r + other.r, g + other.g, b + other.b, a + other.a); }
b8976b
	inline Color operator- (const Color &other) const
b8976b
		{ return Color(r - other.r, g - other.g, b - other.b, a - other.a); }
b8976b
	inline Color operator* (Channel k) const
b8976b
		{ return Color(r*k, g*k, b*k, a*k); }
b8976b
	
b8976b
	inline Color& operator+= (const Color &other)
b8976b
		{ r += other.r, g += other.g, b += other.b, a += other.a; return *this; }
b8976b
	inline Color& operator-= (const Color &other)
b8976b
		{ r -= other.r, g -= other.g, b -= other.b, a -= other.a; return *this; }
b8976b
	inline Color& operator*= (Channel k)
b8976b
		{ r *= k, g *= k, b *= k, a *= k; return *this; }
b8976b
	
b8976b
	inline Color get_mult_alpha() const
b8976b
		{ return Color(r*a, g*a, b*a, a); }
b8976b
	inline Color get_demult_alpha() const  {
b8976b
		if (fabs(a) < real_precision) return *this;
b8976b
		Channel k = 1/a;
b8976b
		return Color(r*k, g*k, b*k, a);
b8976b
	}
b8976b
	
b8976b
	inline void mult_alpha()
b8976b
		{ r*=a, g*=a, b*=a; }
b8976b
	inline void demult_alpha() {
b8976b
		if (fabs(a) < real_precision) return;
b8976b
		Real k = 1/a;
b8976b
		r*=k, g*=k, b*=k;
b8976b
	}
b8976b
};
b8976b
b8976b
b8976b
inline bool real_less(const Real &a, const Real &b)
b8976b
	{ return a < b - real_precision; }
b8976b
inline bool real_greater(const Real &a, const Real &b)
b8976b
	{ return real_less(b, a); }
b8976b
inline bool real_less_or_equal(const Real &a, const Real &b)
b8976b
	{ return !real_less(b, a); }
b8976b
inline bool real_greater_or_equal(const Real &a, const Real &b)
b8976b
	{ return !real_less(a, b); }
b8976b
inline bool real_equal(const Real &a, const Real &b)
b8976b
	{ return !real_less(a, b) && !real_less(b, a); }
b8976b
inline bool real_not_equal(const Real &a, const Real &b)
b8976b
	{ return real_less(a, b) || real_less(b, a); }
b8976b
b8976b
	
b8976b
inline int clz(const Int &x)
b8976b
	{ return x ? __builtin_clz(x) : 32; }
b8976b
inline int clz(const UInt &x)
b8976b
	{ return x ? __builtin_clz(x) : 32; }
b8976b
inline int clz(const LongInt &x)
b8976b
	{ return x ? __builtin_clzll(x) : 64; }
b8976b
inline int clz(const ULongInt &x)
b8976b
	{ return x ? __builtin_clzll(x) : 64; }
b8976b
b8976b
b8976b
inline Real clamp(Real x, Real min, Real max)
b8976b
	{ return std::max(min, std::min(max, x)); }
b8976b
inline int iclamp(int x, int min, int max)
b8976b
	{ return std::max(min, std::min(max, x)); }
b8976b
	
b8976b
inline Real flip_clamp(Real x) {
b8976b
	x -= floor(0.5*x)*2.0;
b8976b
	return 1.0 - fabs(1.0 - x);
b8976b
};
b8976b
b8976b
#endif