<commit_before>package gg

import (
	"image/color"
	"math"
	"sort"
)

type stop struct {
	pos   float64
	color color.Color
}

type stops []stop

// Len satisfies the Sort interface.
func (s stops) Len() int {
	return len(s)
}

// Less satisfies the Sort interface.
func (s stops) Less(i, j int) bool {
	return s[i].pos < s[j].pos
}

// Swap satisfies the Sort interface.
func (s stops) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

type Gradient interface {
	Pattern
	AddColorStop(offset float64, color color.Color)
}

// Linear Gradient
type linearGradient struct {
	x0, y0, x1, y1 float64
	dx, dy         float64
	mag            float64
	px0, py0       float64
	stops          stops
}

func (g *linearGradient) ColorAt(x, y int) color.Color {
	if len(g.stops) == 0 {
		return color.Transparent
	}

	fx, fy := float64(x), float64(y)
	x0, y0 := g.x0, g.y0
	dx, dy := g.dx, g.dy

	// Horizontal
	if dy == 0 && dx != 0 {
		return getColor((fx-x0)/dx, g.stops)
	}

	// Vertical
	if dx == 0 && dy != 0 {
		return getColor((fy-y0)/dy, g.stops)
	}

	px0, py0 := g.px0, g.py0
	mag := g.mag

	s0 := (px0-x0)*(fy-y0) - (py0-y0)*(fx-x0)
	if s0 > 0 {
		return g.stops[0].color
	}
	u := ((fx-x0)*(px0-x0) + (fy-y0)*(py0-y0)) / (mag * mag)
	x2, y2 := x0+u*(px0-x0), y0+u*(py0-y0)
	d := math.Hypot(fx-x2, fy-y2) / mag
	return getColor(d, g.stops)
}

func (g *linearGradient) AddColorStop(offset float64, color color.Color) {
	g.stops = append(g.stops, stop{pos: offset, color: color})
	sort.Sort(g.stops)
}

func NewLinearGradient(x0, y0, x1, y1 float64) Gradient {
	dx, dy := x1-x0, y1-y0
	mag := math.Hypot(dx, dy)
	px0, py0 := x0-dy, y0+dx
	g := &linearGradient{
		x0: x0, y0: y0,
		x1: x1, y1: y1,
		dx: dx, dy: dy,
		px0: px0, py0: py0,
		mag: mag,
	}
	return g
}

func getColor(pos float64, stops stops) color.Color {
	if pos <= 0.0 || len(stops) == 1 {
		return stops[0].color
	}

	last := stops[len(stops)-1]

	if pos >= last.pos {
		return last.color<commit_msg>cleanup, add commets<commit_after>package gg

import (
	"image/color"
	"math"
	"sort"
)

type stop struct {
	pos   float64
	color color.Color
}

type stops []stop

// Len satisfies the Sort interface.
func (s stops) Len() int {
	return len(s)
}

// Less satisfies the Sort interface.
func (s stops) Less(i, j int) bool {
	return s[i].pos < s[j].pos
}

// Swap satisfies the Sort interface.
func (s stops) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

type Gradient interface {
	Pattern
	AddColorStop(offset float64, color color.Color)
}

// Linear Gradient
type linearGradient struct {
	x0, y0, x1, y1 float64
	stops          stops
}

func (g *linearGradient) ColorAt(x, y int) color.Color {
	if len(g.stops) == 0 {
		return color.Transparent
	}

	fx, fy := float64(x), float64(y)
	x0, y0, x1, y1 := g.x0, g.y0, g.x1, g.y1
	dx, dy := x1-x0, y1-y0

	// Horizontal
	if dy == 0 && dx != 0 {
		return getColor((fx-x0)/dx, g.stops)
	}

	// Vertical
	if dx == 0 && dy != 0 {
		return getColor((fy-y0)/dy, g.stops)
	}

	// Dot product
	s0 := dx*(fx-x0) + dy*(fy-y0)
	if s0 < 0 {
		return g.stops[0].color
	}
	// Calculate distance to (x0,y0) alone (x0,y0)->(x1,y1)
	mag := math.Hypot(dx, dy)
	u := ((fx-x0)*-dy + (fy-y0)*dx) / (mag * mag)
	x2, y2 := x0+u*-dy, y0+u*dx
	d := math.Hypot(fx-x2, fy-y2) / mag
	return getColor(d, g.stops)
}

func (g *linearGradient) AddColorStop(offset float64, color color.Color) {
	g.stops = append(g.stops, stop{pos: offset, color: color})
	sort.Sort(g.stops)
}

func NewLinearGradient(x0, y0, x1, y1 float64) Gradient {
	g := &linearGradient{
		x0: x0, y0: y0,
		x1: x1, y1: y1,
	}
	return g
}

func getColor(pos float64, stops stops) color.Color {
	if pos <= 0.0 || len(stops) == 1 {
		return stops[0].color
	}

	last := stops[len(stops)-1]

	if pos >= last.pos {
		return last.color
{'fraction_non_alphanumeric': 0.17343957503320054, 'fraction_numerical': 0.046215139442231074, 'mean_word_length': 3.0235042735042734, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6904116', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1680, 'n_words': 445}