Skip to content

Commit 9bdab55

Browse files
committed
Add free function trig, color channel accessors (sin/cos/degrees/radians/red/green/blue/alpha/hue/saturation/brightness)
1 parent faa4fbf commit 9bdab55

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/Processing.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3662,6 +3662,24 @@ template<class A> inline float sqrt(A v){ return ::std::sqrt((float)v); }
36623662
template<class A,class B> inline float pow(A base,B exp){ return ::std::pow((float)base,(float)exp); }
36633663
template<class A> inline float exp(A v){ return ::std::exp((float)v); }
36643664
template<class A> inline float log(A v){ return ::std::log((float)v); }
3665+
// Trig
3666+
inline float sin(float x) { return ::std::sin(x); }
3667+
inline float cos(float x) { return ::std::cos(x); }
3668+
inline float tan(float x) { return ::std::tan(x); }
3669+
inline float asin(float x) { return ::std::asin(x); }
3670+
inline float acos(float x) { return ::std::acos(x); }
3671+
inline float atan(float x) { return ::std::atan(x); }
3672+
template<class A,class B> inline float atan2(A y,B x){ return ::std::atan2((float)y,(float)x); }
3673+
inline float degrees(float r) { return r*180.f/PI; }
3674+
inline float radians(float d) { return d*PI/180.f; }
3675+
// Color channel accessors
3676+
inline float alpha(color c) { return (float)((c.value>>24)&0xFF); }
3677+
inline float red(color c) { return (float)((c.value>>16)&0xFF); }
3678+
inline float green(color c) { return (float)((c.value>>8)&0xFF); }
3679+
inline float blue(color c) { return (float)(c.value&0xFF); }
3680+
inline float brightness(color c) { float r=red(c)/255.f,g=green(c)/255.f,b=blue(c)/255.f; return ::std::max({r,g,b})*255.f; }
3681+
inline float hue(color c) { float r=red(c)/255.f,g=green(c)/255.f,b=blue(c)/255.f; float mx=::std::max({r,g,b}),mn=::std::min({r,g,b}); if(mx==mn) return 0; float h=0; if(mx==r) h=(g-b)/(mx-mn); else if(mx==g) h=2+(b-r)/(mx-mn); else h=4+(r-g)/(mx-mn); h*=60; if(h<0) h+=360; return h/360.f*255.f; }
3682+
inline float saturation(color c) { float r=red(c)/255.f,g=green(c)/255.f,b=blue(c)/255.f; float mx=::std::max({r,g,b}),mn=::std::min({r,g,b}); return mx==0?0:(mx-mn)/mx*255.f; }
36653683

36663684
// image() -- mixed types, value and pointer variants
36673685
// image() -- draw a PImage to screen

0 commit comments

Comments
 (0)