Name

green()

Description

Extracts the green value from a color, scaled to match current colorMode(). The value is always returned as a float, so be careful not to assign it to an int value.

The green() function is easy to use and understand, but it is slower than a technique called bit shifting. When working in colorMode(RGB, 255), you can achieve the same results as green() but with greater speed by using the right shift operator (>>) with a bit mask. For example, the following two lines of code are equivalent means of getting the green value of the color value c:

 float g1 = green(c); // Simpler, but slower to calculate
 float g2 = c >> 8 & 0xFF; // Very fast to calculate 

Examples

  • size(400, 400);
    color c = color(20, 75, 200);  // Define color 'c'
    fill(c);  // Use color variable 'c' as fill color
    rect(60, 80, 140, 240);  // Draw left rectangle
    
    float greenValue = green(c);  // Get green in 'c'
    println(greenValue);  // Print "75.0"
    fill(0, greenValue, 0);  // Use 'greenValue' in new fill
    rect(200, 80, 140, 240);  // Draw right rectangle
    Image output for example 1

Syntax

  • green(rgb)

Parameters

  • rgb(int)any value of the color datatype

Return

  • float
Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.