Class: Wasko::Palette::TheOriginal

Inherits:
Object
  • Object
show all
Defined in:
lib/wasko/palette.rb

Overview

Since I change my mind pretty frequently on color schemes etc. Putting the actual logic in a class so it’s easier to extend.

Instance Method Summary collapse

Constructor Details

#initialize(color_string, contrast = 0) ⇒ TheOriginal

Takes a valid css color string and an optional contrast argument. The contrast is useful to tweak the output of things.

It sets a base color ‘@base` and creates a color palette derived from the base color using the `colors!` method.



16
17
18
19
20
21
22
# File 'lib/wasko/palette.rb', line 16

def initialize(color_string, contrast = 0)
  @colors = {}
  @base = Wasko::Color.color_from_string(color_string)
  @base = white unless @base
  @contrast = 30 + contrast
  colors!
end

Instance Method Details

#ansi_colors?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/wasko/palette.rb', line 34

def ansi_colors?
  p.colors[:yellow]
end

#base_color_with_tint(color_name) ⇒ Object



98
99
100
101
# File 'lib/wasko/palette.rb', line 98

def base_color_with_tint(color_name)
  brightness = inverse_brightness * @contrast
  mix_base_with(color_name, 80, brightness)
end

#blackObject

Returns a ‘color`-instance of black



30
31
32
# File 'lib/wasko/palette.rb', line 30

def black
  Wasko::Color.color_from_string("black")
end

#colorsObject

Hash of the color palette TODO: attr_accessible



60
61
62
# File 'lib/wasko/palette.rb', line 60

def colors
  @colors
end

#colors!Object

Creates a palette based on the ‘@base`-color. This generates a color palette which has taken a good look at [Solarized](ethanschoonover.com/solarized) The plus side is you can use any base color, the downside is, the colors won’t be picked as well as when using [Solarized](ethanschoonover.com/solarized) so if that’s what you need, check it out.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/wasko/palette.rb', line 70

def colors!
  p = {}
  p[:base]       = @base
  p[:foreground] = @base.mix_with(opposite_color, @contrast + 18)
  p[:bold]       = @base.mix_with(opposite_color, @contrast + 19.5)
  p[:selection]  = @base.adjust_brightness inverse_brightness * @contrast
  p[:selected]   = p[:bold]
  p[:cursor]     = p[:foreground]

  # ANSI Colors
  p[:red]     = mix_base_with("red", 50, inverse_brightness * @contrast)
  p[:green]   = mix_base_with("green", 50, inverse_brightness * @contrast)
  p[:yellow]  = mix_base_with("yellow", 50, inverse_brightness * @contrast)
  p[:white]   = mix_base_with("white", 35, inverse_brightness * @contrast)
  p[:black]   = mix_base_with("black", 35, inverse_brightness * @contrast)
  p[:blue]    = mix_base_with("blue", 50, inverse_brightness * (@contrast))
  p[:magenta] = mix_base_with("#CA1F7B", 35, inverse_brightness * @contrast)
  p[:cyan]    = mix_base_with("#00FFFF", 50, inverse_brightness * (@contrast - 20))
  @colors = p
end

#dark_base_color?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/wasko/palette.rb', line 54

def dark_base_color?
  @base.brightness < 0.5
end

#inverse_brightnessObject

To calculate colors that will fit our base color we need to find out to brighten them, or darken them



50
51
52
# File 'lib/wasko/palette.rb', line 50

def inverse_brightness
  @base.brightness > 0.5 ? -1 : 1
end

#mix_base_with(color_name, mix_value = 50, brightness = 30) ⇒ Object

Just a utility method that mixes colors, for more info on this check the docs of the ‘color`-gem.



93
94
95
# File 'lib/wasko/palette.rb', line 93

def mix_base_with(color_name, mix_value = 50, brightness = 30)
  @base.mix_with(Wasko::Color.color_from_string(color_name), mix_value).adjust_brightness(brightness)
end

#opposite_colorObject

Checks the brightness of the base color and returns the appropriate opposite color.

For example black will return white, white will return black.



43
44
45
# File 'lib/wasko/palette.rb', line 43

def opposite_color
  @base.brightness > 0.5 ? black : white
end

#whiteObject

Returns a ‘color`-instance of white



25
26
27
# File 'lib/wasko/palette.rb', line 25

def white
  Wasko::Color.color_from_string("white")
end