// Copyright (c) 2024 Tulir Asokan // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. package rainbow import ( "regexp" "strings" "github.com/lucasb-eyer/go-colorful" ) // GradientTable from https://github.com/lucasb-eyer/go-colorful/blob/master/doc/gradientgen/gradientgen.go type GradientTable []struct { Col colorful.Color Pos float64 } func (gt GradientTable) GetInterpolatedColorFor(t float64) colorful.Color { for i := 0; i < len(gt)-1; i++ { c1 := gt[i] c2 := gt[i+1] if c1.Pos <= t && t <= c2.Pos { t := (t - c1.Pos) / (c2.Pos - c1.Pos) return c1.Col.BlendHcl(c2.Col, t).Clamped() } } return gt[len(gt)-1].Col } var Gradient = GradientTable{ {colorful.LinearRgb(1, 0, 0), 0 / 11.0}, {colorful.LinearRgb(1, 0.5, 0), 1 / 11.0}, {colorful.LinearRgb(1, 1, 0), 2 / 11.0}, {colorful.LinearRgb(0.5, 1, 0), 3 / 11.0}, {colorful.LinearRgb(0, 1, 0), 4 / 11.0}, {colorful.LinearRgb(0, 1, 0.5), 5 / 11.0}, {colorful.LinearRgb(0, 1, 1), 6 / 11.0}, {colorful.LinearRgb(0, 0.5, 1), 7 / 11.0}, {colorful.LinearRgb(0, 0, 1), 8 / 11.0}, {colorful.LinearRgb(0.5, 0, 1), 9 / 11.0}, {colorful.LinearRgb(1, 0, 1), 10 / 11.0}, {colorful.LinearRgb(1, 0, 0.5), 11 / 11.0}, } func ApplyColor(htmlBody string) string { count := strings.Count(htmlBody, defaultRB.ColorID) i := -1 return regexp.MustCompile(defaultRB.ColorID).ReplaceAllStringFunc(htmlBody, func(match string) string { i++ return Gradient.GetInterpolatedColorFor(float64(i) / float64(count)).Hex() }) }