package lipglossimport ()// Blend1D blends a series of colors together in one linear dimension using multiple// stops, into the provided number of steps. Uses the "CIE L*, a*, b*" (CIELAB) color-space.//// Note that if any of the provided colors are completely transparent, we will// assume that the alpha value was lost in conversion from RGB -> RGBA, and we// will set the alpha to opaque, as it's not possible to blend something completely// transparent.func ( int, ...color.Color) []color.Color {if < 0 { = 0 }if <= len() {return [:] }// Ensure they didn't provide any nil colors. = slices.DeleteFunc(, func( color.Color) bool {return == nil })iflen() == 0 {returnnil// We can't safely fallback. }// If they only provided one valid color (or some nil colors), we will just return // an array of that color, for the amount of steps they requested.iflen() == 1 { := [0] := make([]color.Color, )for := range { [] = }return } := make([]color.Color, )// Convert stops to colorful.Color once := make([]colorful.Color, len())for , := range { [], _ = colorful.MakeColor(ensureNotTransparent()) } := len() - 1 := / := % := 0for := range { := [] := [+1]// Calculate segment size. := if < { ++ } := float64( - 1)// Generate colors for this segment.for := 0; < ; ++ {varfloat64if > 1 { = float64() / } [] = .BlendLab(, ).Clamped() ++ } }return}// Blend2D blends a series of colors together in two linear dimensions using// multiple stops, into the provided width/height. Uses the "CIE L*, a*, b*" (CIELAB)// color-space. The angle parameter controls the rotation of the gradient (0-360°),// where 0° is left-to-right, 45° is bottom-left to top-right (diagonal). The function// returns colors in a 1D row-major order ([row1, row2, row3, ...]).//// Example of how to iterate over the result://// gradient := colors.Blend2D(width, height, 180, color1, color2, color3, ...)// gradientContent := strings.Builder{}// for y := range height {// for x := range width {// index := y*width + x// gradientContent.WriteString(// lipgloss.NewStyle().// Background(gradient[index]).// Render(" "),// )// }// if y < height-1 { // End of row.// gradientContent.WriteString("\n")// }// }//// Note that if any of the provided colors are completely transparent, we will// assume that the alpha value was lost in conversion from RGB -> RGBA, and we// will set the alpha to opaque, as it's not possible to blend something completely// transparent.func (, int, float64, ...color.Color) []color.Color {if < 1 { = 1 }if < 1 { = 1 }// Normalize angle to 0-360. = math.Mod(, 360)if < 0 { += 360 }// Ensure they didn't provide any nil colors. = slices.DeleteFunc(, func( color.Color) bool {return == nil })iflen() == 0 {returnnil// We can't safely fallback. }// If they only provided one valid color (or some nil colors), we will just return // an array of that color, for the amount of pixels they requested.iflen() == 1 { := [0] := make([]color.Color, *)for := range { [] = }return }// For 2D blending, we'll create a gradient along the diagonal and then sample // from it based on the angle. We'll use the maximum dimension to ensure we have // enough resolution for the gradient. := Blend1D(max(, ), ...) := make([]color.Color, *)// Calculate center point for rotation. := float64(-1) / 2.0 := float64(-1) / 2.0 := * math.Pi / 180.0// -> radians.// Pre-calculate sin and cos. := math.Cos() := math.Sin()// Calculate diagonal length for proper gradient mapping. := math.Sqrt(float64(* + *))// Pre-calculate gradient length for index calculation. := float64(len() - 1)for := range {// Calculate the distance from center along the gradient direction. := float64() - for := 0; < ; ++ {// Calculate the distance from center along the gradient direction. := float64() - := * - * // Rotate the point by the angle.// Map the rotated position to the gradient. Normalize to 0-1 range based on // the diagonal length. := clamp((+/2.0)/, 0, 1)// Calculate the index in the gradient. := int( * )if >= len() { = len() - 1 } [*+] = [] // -> row-major order. } }return}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)