def swatch(self, x, y, w=<NUM_LIT>, h=<NUM_LIT>, padding=<NUM_LIT:4>, roundness=<NUM_LIT:0>, n=<NUM_LIT:12>, d=<NUM_LIT>, grouped=None):
if grouped is None:  <EOL><INDENT>grouped = self.group_swatches<EOL><DEDENT>if not grouped:<EOL><INDENT>s = sum([wgt for clr, rng, wgt in self.ranges])<EOL>for clr, rng, wgt in self.ranges:<EOL><INDENT>cols = max(<NUM_LIT:1>, int(wgt / s * n))<EOL>for i in _range(cols):<EOL><INDENT>rng.colors(clr, n=n, d=d).swatch(x, y, w, h, padding=padding, roundness=roundness)<EOL>x += w + padding<EOL><DEDENT><DEDENT>return x, y + n * (h + padding)<EOL><DEDENT>grouped = self._weight_by_hue()<EOL>for total_weight, normalized_weight, hue, ranges in grouped:<EOL><INDENT>dy = y<EOL>rc = <NUM_LIT:0><EOL>for clr, rng, weight in ranges:<EOL><INDENT>dx = x<EOL>cols = int(normalized_weight * n)<EOL>cols = max(<NUM_LIT:1>, min(cols, n - len(grouped)))<EOL>if clr.name == "<STR_LIT>": rng = rng.black<EOL>if clr.name == "<STR_LIT>": rng = rng.white<EOL>for i in _range(cols):<EOL><INDENT>rows = int(weight / total_weight * n)<EOL>rows = max(<NUM_LIT:1>, rows)<EOL>if (clr, rng, weight) == ranges[-<NUM_LIT:1>] and rc + rows < n: rows += <NUM_LIT:1><EOL>rng.colors(clr, n=rows, d=d).swatch(dx, dy, w, h, padding=padding, roundness=roundness)<EOL>dx += w + padding<EOL><DEDENT>dy += (w + padding) * rows  <EOL>rc = rows<EOL><DEDENT>x += (w + padding) * cols + padding<EOL><DEDENT>return x, dy<EOL>
Draws a weighted swatch with approximately n columns and rows.

When the grouped parameter is True, colors are grouped in blocks of the same hue
(also see the _weight_by_hue() method).