red()

Extracts the red value from a color, scaled to match current color_mode().

Examples

example picture for red()
1
2
3
4
5
6
7
8
9
def setup():
    c = "#FFCC00"  # define color 'c'
    py5.fill(c)  # use color variable 'c' as fill color
    py5.rect(15, 20, 35, 60)  # draw left rectangle

    red_value = py5.red(c)  # get red in 'c'
    py5.println(red_value)  # print "255.0"
    py5.fill(red_value, 0, 0)  # use 'red_value' in new fill
    py5.rect(50, 20, 35, 60)  # draw right rectangle

Description

Extracts the red value from a color, scaled to match current color_mode().

The red() function is easy to use and understand, but it is slower than a technique called bit shifting. When working in color_mode(RGB, 255), you can achieve the same results as red() but with greater speed by using the right shift operator (>>) with a bit mask. For example, red(c) and c >> 16 & 0xFF both extract the red value from a color variable c but the later is faster.

Underlying Java method: red

Syntax

red(rgb: int, /) -> float

Parameters

  • rgb: int - any value of the color datatype

Updated on September 11, 2021 16:51:34pm UTC