Skip to content

Shuffling & Mixing Sequences

Shuffling for categorical data

Sequences built by interpolation place similar colors next to each other, which can make them hard to distinguish in categorical plots. alternate_colors interleaves the two halves of a sequence so adjacent colors are maximally spread apart.

For a sequence of N colors the resulting order is: 1, N/2+1, 2, N/2+2, …, N/2, N.

from polychromos.palette import Palette

shuffled = Palette.alternate_colors(sequence)

Mixing two sequences

mix_color_sequences picks colors from two sequences according to a list of boolean selectors. False picks from sequence A, True picks from sequence B.

from polychromos.color.web import get_web_color

seq_a = [get_web_color('silver'), get_web_color('gray')]
seq_b = [get_web_color('crimson'), get_web_color('gold'), get_web_color('seagreen')]

selectors = [False, True, True, False, True, False, True, False]

Two indexing strategies control which color is picked from each source sequence:

BY_POSITION

The color index matches the position in the result sequence. If the index exceeds the sequence length, it wraps around.

# Selector: False,  True,     True,     False, True,  False, True,    False
# Index A:  0,      1,        0,        1,     0,     1,     0,       1
# Index B:  0,      1,        2,        0,     1,     2,     0,       1
# Color:    silver, gold,     seagreen, gray,  gold,  gray,  crimson, gray
result = Palette.mix_color_sequences(seq_a, seq_b, selectors,
                                     indexing=Palette.MixIndexing.BY_POSITION)

BY_USE

Colors are drawn iteratively from each sequence independently, so each sequence advances its own pointer only when it is selected. Wraps around when exhausted.

# Selector: False,  True,    True, False, True,     False,  True,    False
# Index A:  0,      -,       -,    1,     -,        0,      -,       1
# Index B:  -,      0,       1,    -,     2,        -,      0,       -
# Color:    silver, crimson, gold, gray,  seagreen, silver, crimson, gray
result = Palette.mix_color_sequences(seq_a, seq_b, selectors,
                                     indexing=Palette.MixIndexing.BY_USE)