background()

Color / Setting

Description

Sets the color used for the background of the Processing window. Called in draw() to clear the canvas each frame.

Syntax

void background(float gray) void background(float gray, float a) void background(float r, float g, float b) void background(float r, float g, float b, float a) void background(color c) void background(const PImage& img)

Parameters

NameTypeDescription
grayfloatgrayscale value (0-255)
rfloatred component
gfloatgreen component
bfloatblue component
afloatalpha (optional)
imgPImageimage to use as background

Returns

void

Related

Under the Hood

From Processing.h:

void background(const PColor& c); void background(float g); void background(float g); void background(float r, float g, float b); void background(float g); void background(float r, float g, float b); void background(float r, float g, float b, float a); void background(float); void background(float,float); void background(float,float,float); void background(float,float,float,float); { _api::background((float)gray); { _api::background((float)gray,(float)a); { _api::background((float)r,(float)g,(float)b); { _api::background((float)r,(float)g,(float)b,(float)a); void background(float gray, float a=255.f); void background(float r, float g, float b, float a=255.f); void background(color c); void background(const PImage& img); void background(const PImage* img) { if(img) background(*img); } void background(const PImage* img) { if(img) background(*img); template<typename A, typename=std::enable_if_t<std::is_arithmetic_v<A>>> void background(A gray) { background((float)gray); template<typename A, typename B, typename=std::enable_if_t<std::is_arithmetic_v<A>&&std::is_arithmetic_v<B>>> void background(A gray, B a) { background((float)gray,(float)a); template<typename A, typename B, typename C, typename=std::enable_if_t<std::is_arithmetic_v<A>&&std::is_arithmetic_v<B>&&std::is_arithmetic_v<C>>> void background(A r, B g, C b) { background((float)r,(float)g,(float)b); template<typename A, typename B, typename C, typename D, typename=std::enable_if_t<std::is_arithmetic_v<A>&&std::is_arithmetic_v<B>&&std::is_arithmetic_v<C>&&std::is_arithmetic_v<D>>> void background(A r, B g, C b, D a) { background((float)r,(float)g,(float)b,(float)a); inline void background(float g){ if(PApplet::g_papplet) PApplet::g_papplet->background(g); } inline void background(float g,float a){ if(PApplet::g_papplet) PApplet::g_papplet->background(g,a); } inline void background(float r,float g,float b){ if(PApplet::g_papplet) PApplet::g_papplet->background(r,g,b); } inline void background(float r,float g,float b,float a){ if(PApplet::g_papplet) PApplet::g_papplet->background(r,g,b,a); } inline void PGraphics::background(float g) { if(PApplet::g_papplet) PApplet::g_papplet->background(g); } inline void PGraphics::background(float r, float g2, float b) { if(PApplet::g_papplet) PApplet::g_papplet->background(r,g2,b,255); }

Under the Hood

From Processing.cpp:

void PApplet::background(float gray, float a) { _backgroundCalledThisFrame = true; color c = makeColor(gray, a); unsigned int v = c.value; setBg((v>>16&0xFF)/255.f, (v>>8&0xFF)/255.f, (v&0xFF)/255.f, (v>>24&0xFF)/255.f); } void PApplet::background(float r, float g, float b, float a) { _backgroundCalledThisFrame = true; color c = makeColor(r, g, b, a); unsigned int v = c.value; setBg((v>>16&0xFF)/255.f, (v>>8&0xFF)/255.f, (v&0xFF)/255.f, (v>>24&0xFF)/255.f); } void PApplet::background(color c) { _backgroundCalledThisFrame = true; unsigned int v = c.value; setBg((v>>16&0xFF)/255.f, (v>>8&0xFF)/255.f, (v&0xFF)/255.f, (v>>24&0xFF)/255.f); } void PApplet::background(const PImage& img) { _backgroundCalledThisFrame = true; // Draw image as full-canvas background if (img.width == 0 || img.height == 0) return; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0, logicalW, logicalH, 0, -1, 1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glDisable(GL_DEPTH_TEST); drawImageRect(const_cast<PImage&>(img), 0, 0, (float)logicalW, (float)logicalH); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); if (defaultP3D) glEnable(GL_DEPTH_TEST); } void PApplet::background(const PColor& c){ background(c.r, c.g, c.b, c.a); } void PApplet::background(const PColor& c){ background(c.r, c.g, c.b, c.a);