1
0
mirror of https://github.com/alecthomas/chroma.git synced 2026-05-22 10:15:46 +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
+3 -2
View File
@@ -18,13 +18,14 @@ import (
) )
var ( 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() listFlag = kingpin.Flag("list", "List lexers, styles and formatters.").Bool()
lexerFlag = kingpin.Flag("lexer", "Lexer to use when formatting.").Default("autodetect").Short('l').String() 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() 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() 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() { func main() {
+3 -29
View File
@@ -5,8 +5,6 @@ import (
"io" "io"
"math" "math"
"github.com/lucasb-eyer/go-colorful"
"github.com/alecthomas/chroma" // nolint "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/styles" "github.com/alecthomas/chroma/styles"
) )
@@ -14,7 +12,6 @@ import (
type ttyTable struct { type ttyTable struct {
foreground map[styles.Colour]string foreground map[styles.Colour]string
background map[styles.Colour]string background map[styles.Colour]string
distance map[styles.Colour]colorful.Color
} }
var c = styles.ParseColour 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 { func entryToEscapeSequence(table *ttyTable, entry *styles.Entry) string {
out := "" out := ""
if entry.Bold { if entry.Bold {
@@ -211,10 +186,9 @@ func entryToEscapeSequence(table *ttyTable, entry *styles.Entry) string {
func findClosest(table *ttyTable, colour styles.Colour) styles.Colour { func findClosest(table *ttyTable, colour styles.Colour) styles.Colour {
closestColour := styles.Colour(0) closestColour := styles.Colour(0)
seeking := toColourful(colour) closest := math.MaxInt32
closest := float64(math.MaxFloat64) for styleColour := range table.foreground {
for styleColour, colour := range table.distance { distance := styleColour.Distance(colour)
distance := colour.DistanceCIE76(seeking)
if distance < closest { if distance < closest {
closest = distance closest = distance
closestColour = styleColour closestColour = styleColour