def id_to_rgb(cls, id): """ Cover float[-2**20~2**20] to float32[0.~1.] rgb Parameters ---------- id : int, float or numpy float[-2**20~2**20]. Returns ------- (3,)float32 RGB for Blender. """ if isinstance(id, (float, int)): id = np.array(id) rgb = np.zeros(id.shape + (3, ), dtype=np.float64) rgb[..., 0] = id < 0 absf = np.abs(id) int_part = absf // 1 # rgb[..., 1] = 1 - 1 / (int_part + 1) poww = np.int32(np.log2(int_part + 1, dtype=np.float32)) + 1 denominator = (2**poww).round() rgb[..., 1] = ((int_part - denominator // 2 + 1) * 2 + 1) / denominator rgb[..., 2] = absf % 1 return rgb
def id_to_rgb(f): if isinstance(f, (float, int)): f = np.array(f) rgb = np.zeros(f.shape + (3, ), dtype=np.float64) rgb[..., 0] = f < 0 absf = np.abs(f) int_part = absf // 1 rgb[..., 1] = 1 - 1 / (int_part + 1) rgb[..., 2] = absf % 1 return rgb
def test_heatmap_to_pseudo_color(depth): def _f(v): return tuple(interpolate_or_clip(turbo_colormap_data, v)) vf = np.vectorize(_f) with timeit("vf"): color1 = np.concatenate([i[..., None] for i in vf(depth)], -1) with timeit("heatmap_to_pseudo_color"): color2 = heatmap_to_pseudo_color(depth) mg() assert (np.abs(color1 - color2) < 1 / 256).all() show - color1