Skip to contents

Get the median (or mean or user-defined function) colour value of a specified patch of pixels on an image. This is useful for matching background colours.

Usage

patch(
  stimuli,
  width = 10,
  height = 10,
  x_off = 0,
  y_off = 0,
  color = c("hex", "rgb"),
  func = stats::median
)

Arguments

stimuli

list of stimuli

width, height

dimensions of the patch in pixels, if <=1, interpreted as proportions of width or height

x_off, y_off

offset in pixels or proportion (<1)

color

The type of color to return (hex, rgb)

func

The function to apply to an array of L*ab color values to determine the central colour (defaults to median, but mean, min, or max can also be useful)

Value

a vector of hex or rgba color values

Details

The colour values of each pixel in the patch are converted to CIE-Lab values before using the func to calculate the central tendency of the L (lightness), a (red-green axis) and b (blue-yellow axis); see col2lab() and lab2rgb() for more details.

This excludes transparent pixels, and returns "transparent" if all pixels in the patch are transparent.

Examples

stimuli <- demo_stim()

# get colour from the upper left corder
patch(stimuli)
#>     f_multi     m_multi 
#> "#B7BBBCFF" "#AAAEADFF" 

# get median colour from centre .1 width pixels
patch(stimuli, width = .1, height = .1, 
      x_off = .45, y_off = .45)
#>     f_multi     m_multi 
#> "#957463FF" "#8D6A5DFF" 

# get mean rgb colour from full image
patch(stimuli, width = 1, height = 1, 
      color = "rgb", func = mean)
#> $f_multi
#>   red green  blue alpha 
#>   187   181   178   255 
#> 
#> $m_multi
#>   red green  blue alpha 
#>   189   185   182   255 
#>