solarized-emacs

a fork of Bozhidar Batsov's solarized-emacs
git clone https://git.trogloxene.org/solarized-emacs.git
Log | Files | Refs | README

commit 6e42db9152442db6247ff2d8da652ce07277649a
parent 1f8f78f425cc6a2639e9cedd34ac5e420bcf2afc
Author: Thomas Frössman <thomasf@jossystem.se>
Date:   Wed, 20 Nov 2019 18:02:31 +0100

colorlab: refactor: move colorfinder to colorlab package

Diffstat:
Mcolorlab/main.go | 16++++++++--------
Acolorlab/pkg/colorlab/colorfinder.go | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Mcolorlab/pkg/colorlab/namedcolors.go | 30++++++++++++++++++++++++++++++
Mcolorlab/pkg/generator/accentpair.go | 9+++++----
Mcolorlab/pkg/solarized/base.go | 15+--------------
Dcolorlab/pkg/solarized/colorfinder.go | 50--------------------------------------------------
6 files changed, 93 insertions(+), 76 deletions(-)

diff --git a/colorlab/main.go b/colorlab/main.go @@ -54,16 +54,16 @@ var ( BlendForegroundAmout: 0.3, Gamma: 0.01, MinimumLightnessDifference: 0.35, - ForegroundBlendFinder: sol.ExtremeColorFgFinder, - BackgroundBlendFinder: sol.ExtremeColorBgFinder, + ForegroundBlendFinder: colorlab.ExtremeColorFgFinder, + BackgroundBlendFinder: colorlab.ExtremeColorBgFinder, } default2BgFg = generator.AccentPairGenerator{ BlendBackgroundAmout: 0.6, BlendForegroundAmout: 0.45, Gamma: 0.04, MinimumLightnessDifference: 0.35, - ForegroundBlendFinder: sol.ExtremeColorFgFinder, - BackgroundBlendFinder: sol.ExtremeColorBgFinder, + ForegroundBlendFinder: colorlab.ExtremeColorFgFinder, + BackgroundBlendFinder: colorlab.ExtremeColorBgFinder, } palettes = []generator.Palette{ @@ -74,7 +74,7 @@ var ( BlendBackgroundAmout: default1BgFg.BlendBackgroundAmout, BlendForegroundAmout: default1BgFg.BlendForegroundAmout, Gamma: default1BgFg.Gamma, - ForegroundBlendFinder: sol.NamedColorFinder("base1"), + ForegroundBlendFinder: colorlab.NamedColorFinder("base1"), BackgroundBlendFinder: default1BgFg.BackgroundBlendFinder, MinimumLightnessDifference: 0.4, }, @@ -82,7 +82,7 @@ var ( BlendBackgroundAmout: default2BgFg.BlendBackgroundAmout, BlendForegroundAmout: default2BgFg.BlendForegroundAmout, Gamma: default2BgFg.Gamma, - ForegroundBlendFinder: sol.NamedColorFinder("base1"), + ForegroundBlendFinder: colorlab.NamedColorFinder("base1"), BackgroundBlendFinder: default2BgFg.BackgroundBlendFinder, MinimumLightnessDifference: 0.4, }, @@ -101,7 +101,7 @@ var ( BlendBackgroundAmout: default1BgFg.BlendBackgroundAmout, BlendForegroundAmout: default1BgFg.BlendForegroundAmout, Gamma: default1BgFg.Gamma, - ForegroundBlendFinder: sol.NamedColorFinder("base1"), + ForegroundBlendFinder: colorlab.NamedColorFinder("base1"), BackgroundBlendFinder: default1BgFg.BackgroundBlendFinder, MinimumLightnessDifference: 0.4, }, @@ -109,7 +109,7 @@ var ( BlendBackgroundAmout: default2BgFg.BlendBackgroundAmout, BlendForegroundAmout: default2BgFg.BlendForegroundAmout, Gamma: default2BgFg.Gamma, - ForegroundBlendFinder: sol.NamedColorFinder("base1"), + ForegroundBlendFinder: colorlab.NamedColorFinder("base1"), BackgroundBlendFinder: default2BgFg.BackgroundBlendFinder, MinimumLightnessDifference: 0.4, }, diff --git a/colorlab/pkg/colorlab/colorfinder.go b/colorlab/pkg/colorlab/colorfinder.go @@ -0,0 +1,49 @@ +package colorlab + +import ( + "log" + + "github.com/lucasb-eyer/go-colorful" +) + +// Find a specific color in a Solarized based on some preconditon +type ColorFinder func(NamedColors) colorful.Color + +// NamedColorFinder extracts a named color from a palette +func NamedColorFinder(name string) ColorFinder { + return func(colors NamedColors) colorful.Color { + c, ok := colors[name] + if !ok { + log.Fatalf("color %s not in list: %v", name, colors) + } + return c.Color() + } +} + +// BgFinder returns base03 from the input Solarized +func BgFinder(nc NamedColors) colorful.Color { + return NamedColorFinder("base03")(nc) + +} + +// FgFinder finds base0 from the input Solarized +func FgFinder(nc NamedColors) colorful.Color { + return NamedColorFinder("base0")(nc) + +} + +// ExtremeColorFgFinder finds the darkest base tone for light on dark or the lightest color for dark on light. +func ExtremeColorFgFinder(nc NamedColors) colorful.Color { + if nc.IsDarkOnBright("base03", "base0") { + return nc.ColorList().ByLightnessDesc()[0] + } + return nc.ColorList().ByLightness()[0] +} + +// ExtremeColorFgFinder finds the lightest base tone for light on dark or the darkest color for dark on light. +func ExtremeColorBgFinder(nc NamedColors) colorful.Color { + if nc.IsDarkOnBright("base03", "base0") { + return nc.ColorList().ByLightness()[0] + } + return nc.ColorList().ByLightnessDesc()[0] +} diff --git a/colorlab/pkg/colorlab/namedcolors.go b/colorlab/pkg/colorlab/namedcolors.go @@ -2,7 +2,10 @@ package colorlab import ( "fmt" + "log" "strings" + + "github.com/lucasb-eyer/go-colorful" ) type NamedColors map[string]HexColor @@ -62,6 +65,33 @@ func (n NamedColors) ColorList() ColorList { return res } +// Returns true if the LAB lightness of bgName is larger than fgName. +func (nc NamedColors) IsDarkOnBright(bgName, fgName string) bool { + lightness := func(c colorful.Color) float64 { + l, _, _ := c.Lab() + return l + } + + // bg, ok := nc["base03"] + bg, ok := nc[bgName] + if !ok { + log.Fatalf("base03 not found in: ", nc) + } + + // fg := nc["base0"] + fg := nc[fgName] + if !ok { + log.Fatalf("base0 not found in: ", nc) + } + + bgl := lightness(bg.Color()) + fgl := lightness(fg.Color()) + if fgl == bgl { + log.Fatalf("bg (%v) and fg (%v) are not supposed to have equal lightness: %v %v", bg, fg, bgl, fgl) + } + return fgl > bgl +} + // Merge merges multiple NamedColors func Merge(nc ...NamedColors) NamedColors { res := make(NamedColors) diff --git a/colorlab/pkg/generator/accentpair.go b/colorlab/pkg/generator/accentpair.go @@ -4,6 +4,7 @@ import ( "fmt" "math" + "github.com/bbatsov/solarized-emacs/colorlab/pkg/colorlab" "github.com/bbatsov/solarized-emacs/colorlab/pkg/solarized" "github.com/lucasb-eyer/go-colorful" @@ -16,8 +17,8 @@ type AccentPairGenerator struct { Gamma float64 MinimumLightnessDifference float64 - BackgroundBlendFinder solarized.ColorFinder - ForegroundBlendFinder solarized.ColorFinder + BackgroundBlendFinder colorlab.ColorFinder + ForegroundBlendFinder colorlab.ColorFinder } func (g *AccentPairGenerator) Generate(sol solarized.Solarized) (backgrounds, foregrounds solarized.Accents) { @@ -35,8 +36,8 @@ func (g *AccentPairGenerator) Generate(sol solarized.Solarized) (backgrounds, fo fmt.Println(" ") } - blendBgColor := g.BackgroundBlendFinder(sol) - blendFgColor := g.ForegroundBlendFinder(sol) + blendBgColor := g.BackgroundBlendFinder(sol.Base.NamedColors()) + blendFgColor := g.ForegroundBlendFinder(sol.Base.NamedColors()) bg := v.BlendLab(blendBgColor, g.BlendBackgroundAmout) fg := v.BlendLab(blendFgColor, g.BlendForegroundAmout) diff --git a/colorlab/pkg/solarized/base.go b/colorlab/pkg/solarized/base.go @@ -1,8 +1,6 @@ package solarized import ( - "log" - "github.com/bbatsov/solarized-emacs/colorlab/pkg/colorlab" "github.com/lucasb-eyer/go-colorful" ) @@ -82,18 +80,7 @@ func (s Base) colorArray() [8]colorlab.HexColor { // Returns true if the LAB lightness of base0 is larger than base03. func (b Base) IsDarkOnBright() bool { - lightness := func(c colorful.Color) float64 { - l, _, _ := c.Lab() - return l - } - bg := b.Base03 - fg := b.Base0 - bgl := lightness(bg.Color()) - fgl := lightness(fg.Color()) - if fgl == bgl { - log.Fatalf("bg (%v) and fg (%v) are not supposed to have equal lightness: %v %v", bg, fg, bgl, fgl) - } - return fgl > bgl + return b.NamedColors().IsDarkOnBright("base03", "base0") } var baseNames = [8]string{ diff --git a/colorlab/pkg/solarized/colorfinder.go b/colorlab/pkg/solarized/colorfinder.go @@ -1,50 +0,0 @@ -package solarized - -import ( - "log" - - "github.com/lucasb-eyer/go-colorful" -) - -// Find a specific color in a Solarized based on some preconditon -type ColorFinder func(Solarized) colorful.Color - -// NamedColorFinder extracts a named color from a palette -func NamedColorFinder(name string) ColorFinder { - return func(sol Solarized) colorful.Color { - colors := sol.NamedColors() - c, ok := colors[name] - if !ok { - log.Fatalf("color %s not in list: %v", name, colors) - } - return c.Color() - } -} - -// BgFinder returns base03 from the input Solarized -func BgFinder(sol Solarized) colorful.Color { - return NamedColorFinder("base03")(sol) - -} - -// FgFinder finds base0 from the input Solarized -func FgFinder(sol Solarized) colorful.Color { - return NamedColorFinder("base0")(sol) - -} - -// ExtremeColorFgFinder finds the darkest base tone for light on dark or the lightest color for dark on light. -func ExtremeColorFgFinder(sol Solarized) colorful.Color { - if sol.IsDarkOnBright() { - return sol.Base.NamedColors().ColorList().ByLightnessDesc()[0] - } - return sol.Base.NamedColors().ColorList().ByLightness()[0] -} - -// ExtremeColorFgFinder finds the lightest base tone for light on dark or the darkest color for dark on light. -func ExtremeColorBgFinder(sol Solarized) colorful.Color { - if sol.IsDarkOnBright() { - return sol.Base.NamedColors().ColorList().ByLightness()[0] - } - return sol.Base.NamedColors().ColorList().ByLightnessDesc()[0] -}