def create_spectrum_from_rgb(r: int, g: int, b: int) -> Spectrum: """ Create a Mitsuba Spectrum from r, g, and b values :param r: Red component :param g: Green component :param b: Blue component :return: A Mitsuba Spectrum """ assert (0 <= r <= 255 and 0 <= g <= 255 and 0 <= b <= 255), "Provide integer rgb values in the range 0-255" spectrum = Spectrum() spectrum.fromSRGB(float(r) / 255., float(g) / 255., float(b) / 255.) return spectrum
def spectrum(self, value, mode=''): if not mode: mode = self.color_mode spec = None if isinstance(value, (dict)): if 'type' in value: if value['type'] in {'rgb', 'srgb', 'spectrum'}: spec = self.spectrum(value['value'], value['type']) elif value['type'] == 'blackbody': spec = Spectrum() spec.fromContinuousSpectrum(BlackBodySpectrum(value['temperature'])) spec.clampNegative() spec = spec * value['scale'] elif isinstance(value, (float, int)): spec = Spectrum(value) elif isinstance(value, (str)): contspec = InterpolatedSpectrum(self.get_export_path(value)) spec = Spectrum() spec.fromContinuousSpectrum(contspec) spec.clampNegative() else: try: items = list(value) for i in items: if not isinstance(i, (float, int, tuple)): raise Exception('Error: spectrum list contains an unknown type') except: items = None if items: totitems = len(items) if isinstance(items[0], (float, int)): if totitems == 3 or totitems == 4: spec = Spectrum() if mode == 'srgb': spec.fromSRGB(items[0], items[1], items[2]) else: spec.fromLinearRGB(items[0], items[1], items[2]) elif totitems == 1: spec = Spectrum(items[0]) else: MtsLog('Expected spectrum items to be 1, 3 or 4, got %d.' % len(items), type(items), items) else: spec = Spectrum() contspec = InterpolatedSpectrum() for spd in items: (wlen, val) = spd contspec.append(wlen, val) spec.fromContinuousSpectrum(contspec) spec.clampNegative() else: MtsLog('Unknown spectrum type.', type(value), value) if spec is None: spec = Spectrum(0.0) return spec
def spectrum(self, value, mode=''): if not mode: mode = self.color_mode spec = None if isinstance(value, (dict)): if 'type' in value: if value['type'] in {'rgb', 'srgb', 'spectrum'}: spec = self.spectrum(value['value'], value['type']) elif value['type'] == 'blackbody': spec = Spectrum() spec.fromContinuousSpectrum( BlackBodySpectrum(value['temperature'])) spec.clampNegative() spec = spec * value['scale'] elif isinstance(value, (float, int)): spec = Spectrum(value) elif isinstance(value, (str)): contspec = InterpolatedSpectrum( self.get_export_path(value)) spec = Spectrum() spec.fromContinuousSpectrum(contspec) spec.clampNegative() else: try: items = list(value) for i in items: if not isinstance(i, (float, int, tuple)): raise Exception( 'Error: spectrum list contains an unknown type' ) except: items = None if items: totitems = len(items) if isinstance(items[0], (float, int)): if totitems == 3 or totitems == 4: spec = Spectrum() if mode == 'srgb': spec.fromSRGB(items[0], items[1], items[2]) else: spec.fromLinearRGB(items[0], items[1], items[2]) elif totitems == 1: spec = Spectrum(items[0]) else: MtsLog( 'Expected spectrum items to be 1, 3 or 4, got %d.' % len(items), type(items), items) else: spec = Spectrum() contspec = InterpolatedSpectrum() for spd in items: (wlen, val) = spd contspec.append(wlen, val) spec.fromContinuousSpectrum(contspec) spec.clampNegative() else: MtsLog('Unknown spectrum type.', type(value), value) if spec is None: spec = Spectrum(0.0) return spec