Skip to content

Lookup Table's Lut.getColor() should use Math.floor instead of Math.round #23549

@dcriado

Description

@dcriado

Describe the bug

In Lut.getColor(), once the input value is converted into an alpha between 0 and 1, alpha is multiplied by the number of colors to assign it an index, and then rounded to the nearest integer using Math.round.

I think this might be wrong, as alpha should be rounded to the nearest integer below, using Math.floor.

Otherwise, values that fall under a color i but are closer to its border with the color i+1 will eventually be assigned the color i+1.

Here is the referred line in the source code:

let colorPosition = Math.round( alpha * this.n );

To Reproduce

See code snippet below.

Code

import { Lut, ColorMapKeywords } from 'three/examples/jsm/math/Lut';

const colormap = ColorMapKeywords.rainbow;
const lut = Lut( colormap, 6 );
    
lut.setMax( 100.0 );
lut.setMin( 0.0 );
    
// This value gets a correct color
// alpha = 0.7
// alpha * nrColors = 4.2
// getColor picks this.lut[4]
const a = 70;
const colorA = lut.getColor( a );

// This value gets a wrong color
// alpha = 0.6
// alpha * nrColors = 3.6
// getColor picks this.lut[4] instead of this.lut[3]
const b = 60;
const colorB = lut.getColor( b );

Screenshots

Maybe this schematic helps to illustrate my point:

imagen

Platform:

  • Device: Desktop
  • OS: Linux
  • Browser: Firefox
  • Three.js version: r137

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions