1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-01-28 03:29:41 +02:00

Just use a basic RGB distance.

Not perfect, but probably sufficient.
This commit is contained in:
Alec Thomas 2017-06-06 20:57:29 +10:00
parent ca512c335b
commit ef4a53333b
2 changed files with 6 additions and 31 deletions

View File

@ -18,13 +18,14 @@ import (
)
var (
profileFlag = kingpin.Flag("profile", "Enable profiling to file.").PlaceHolder("FILE").Hidden().String()
profileFlag = kingpin.Flag("profile", "Enable profiling to file.").Hidden().String()
listFlag = kingpin.Flag("list", "List lexers, styles and formatters.").Bool()
lexerFlag = kingpin.Flag("lexer", "Lexer to use when formatting.").Default("autodetect").Short('l').String()
styleFlag = kingpin.Flag("style", "Style to use for formatting.").Short('s').Default("swapoff").String()
formatterFlag = kingpin.Flag("formatter", "Formatter to use.").Default("terminal").Short('f').String()
filesArgs = kingpin.Arg("files", "Files to highlight.").ExistingFiles()
filesArgs = kingpin.Arg("files", "Files to highlight.").ExistingFiles()
)
func main() {

View File

@ -5,8 +5,6 @@ import (
"io"
"math"
"github.com/lucasb-eyer/go-colorful"
"github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/styles"
)
@ -14,7 +12,6 @@ import (
type ttyTable struct {
foreground map[styles.Colour]string
background map[styles.Colour]string
distance map[styles.Colour]colorful.Color
}
var c = styles.ParseColour
@ -170,28 +167,6 @@ var ttyTables = map[int]*ttyTable{
},
}
func computeDistance(table map[styles.Colour]string) map[styles.Colour]colorful.Color {
out := map[styles.Colour]colorful.Color{}
for colour := range table {
out[colour] = toColourful(colour)
}
return out
}
func init() {
for _, table := range ttyTables {
table.distance = computeDistance(table.foreground)
}
}
func toColourful(colour styles.Colour) colorful.Color {
return colorful.Color{
float64(colour.Red()) / 256.0,
float64(colour.Green()) / 256.0,
float64(colour.Blue()) / 256.0,
}
}
func entryToEscapeSequence(table *ttyTable, entry *styles.Entry) string {
out := ""
if entry.Bold {
@ -211,10 +186,9 @@ func entryToEscapeSequence(table *ttyTable, entry *styles.Entry) string {
func findClosest(table *ttyTable, colour styles.Colour) styles.Colour {
closestColour := styles.Colour(0)
seeking := toColourful(colour)
closest := float64(math.MaxFloat64)
for styleColour, colour := range table.distance {
distance := colour.DistanceCIE76(seeking)
closest := math.MaxInt32
for styleColour := range table.foreground {
distance := styleColour.Distance(colour)
if distance < closest {
closest = distance
closestColour = styleColour