Get a numpy of colors of shape (N, 3).
(number_of_colors,
flag=0,
alpha: float = 1.0,
mode: str = 'bgr',
int_dtype: bool = True)
| 680 | |
| 681 | |
| 682 | def get_different_colors(number_of_colors, |
| 683 | flag=0, |
| 684 | alpha: float = 1.0, |
| 685 | mode: str = 'bgr', |
| 686 | int_dtype: bool = True): |
| 687 | """Get a numpy of colors of shape (N, 3).""" |
| 688 | mode = mode.lower() |
| 689 | assert set(mode).issubset({'r', 'g', 'b', 'a'}) |
| 690 | nst0 = np.random.get_state() |
| 691 | np.random.seed(flag) |
| 692 | colors = [] |
| 693 | for i in np.arange(0., 360., 360. / number_of_colors): |
| 694 | hue = i / 360. |
| 695 | lightness = (50 + np.random.rand() * 10) / 100. |
| 696 | saturation = (90 + np.random.rand() * 10) / 100. |
| 697 | colors.append(colorsys.hls_to_rgb(hue, lightness, saturation)) |
| 698 | colors_np = np.asarray(colors) |
| 699 | if int_dtype: |
| 700 | colors_bgr = (255 * colors_np).astype(np.uint8) |
| 701 | else: |
| 702 | colors_bgr = colors_np.astype(np.float32) |
| 703 | # recover the random state |
| 704 | np.random.set_state(nst0) |
| 705 | color_dict = {} |
| 706 | if 'a' in mode: |
| 707 | color_dict['a'] = np.ones((colors_bgr.shape[0], 3)) * alpha |
| 708 | color_dict['b'] = colors_bgr[:, 0:1] |
| 709 | color_dict['g'] = colors_bgr[:, 1:2] |
| 710 | color_dict['r'] = colors_bgr[:, 2:3] |
| 711 | colors_final = [] |
| 712 | for channel in mode: |
| 713 | colors_final.append(color_dict[channel]) |
| 714 | colors_final = np.concatenate(colors_final, -1) |
| 715 | return colors_final |
| 716 | |
| 717 | |
| 718 | class RunningAverage(): |
no test coverage detected