Looking for insights to suppress some redundancy in a "long" formula

Wait, are you trying to interpolate RGB components of a color independently?

Because doing that won’t give you the best-looking gradients. A more natural way to blend colors is to use HSL or HSV model. Or I imagine there should be even better models where luminance of different colors are normalized in some way (a quick googling gave me HCL. The problem with regular linear RGB transitions is that different components have different perceived lightness, as well as transitions e.g. from green to blue won’t pass through cyan but rather dark cyan (#00FF00 to #0000FF would pass through #008080)

IIRC @Phil_Hamilton did some work in this area:

You could also just have SVG gradients, no need to calculate intermediate colors yourself :slight_smile:


But for general case here’s my take at the algorithm.

The formula:

List(0, 255, 0).WithName(StartList,
List(255, 0, 0).WithName(EndList,
ToNumber(16).WithName(DataStops,
  Min(StartList.Count(), EndList.Count()).WithName(VectorDim,
    
    Sequence(1, VectorDim).FormulaMap(
      StartList.Nth(CurrentValue).WithName(CurrentStart,
      EndList.Nth(CurrentValue).WithName(CurrentEnd,
        Sequence(0, DataStops - 1).FormulaMap(
          CurrentStart + (CurrentEnd - CurrentStart) * (CurrentValue / (DataStops - 1))
        )
      ))
    ).WithName(InterpolatedSequences,
      Sequence(1, DataStops).FormulaMap(CurrentValue.WithName(DataStop,
        Sequence(1, VectorDim).FormulaMap(CurrentValue.WithName(I,
          InterpolatedSequences.Nth(I).Nth(DataStop)
        ))
      ))
    )
    
  )
)))

Note: you only need the inner part if you already have your three lists and the number of data stops, and feel free to replace VectorDim with a literal number 3 if you don’t need this to work with any sized lists.

Video:

2 Likes