Algosim documentation: ComponentHighlight


Highlights the connected components of a pixmap using random colours.



ComponentHighlight(pm) returns pm with each connected background component – defined as a maximal region of background-coloured pixels – filled with a randomly chosen colour.

ComponentHighlight(pm, f) uses f to determine the colours to fill the background components with. f is a function that accepts a positive integer and returns a colour.

Each connected component will get the colour returned by this function, which typically is called once per connected component, with the parameter incremented each invocation and having the value 1 on the first invocation.

ComponentHighlight(pm) is implemented as ComponentHighlight(pm, ξ) where ξ is the standard random colour function that, on each invocation, returns a colour with randomly chosen RGB coordinates.


For each pixel in the image, if the colour of the pixel is equal to the background colour, a flood fill is performed at that pixel using the colour returned by f. Consequently, if this function at some point returns the background colour, it might be called more than once for that component. Since there are 256³ = 16777216 different colour values, the probability that the standard random colour function returns the background colour is slightly less than 0.000006% each time it is called.


a ≔ DrawLines(CreatePixmap(1000, 1000), '(❨200, 0❩, ❨200, 700❩, ❨800, 700❩,
  ❨800, 200❩, ❨100, 200❩, ❨100, 500❩, ❨600, 500❩, ❨600, 1000❩), "black")

Image 1


Image 2

ComponentHighlight(a, n ↦ '("#13031D", "#049CC8", "#89AFCE", "#8AC7A6", "#52E303")[n])

Image 3