Module: Chamomile::ColorProfile

Defined in:
lib/chamomile/styling/color_profile.rb

Constant Summary collapse

TRUE_COLOR =
:true_color
ANSI256 =
:ansi256
ANSI =
:ansi
NO_COLOR =
:no_color
ANSI256_TO_ANSI =

ANSI256 to ANSI16 lookup table

[
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, # 0-15: identity
  0, 4, 4, 4, 12, 12,   # 16-21
  2, 6, 4, 4, 12, 12,   # 22-27
  2, 2, 6, 4, 12, 12,   # 28-33
  2, 2, 2, 6, 12, 12,   # 34-39
  10, 10, 10, 10, 14, 12, # 40-45
  10, 10, 10, 10, 10, 14, # 46-51
  1, 5, 4, 4, 12, 12,   # 52-57
  3, 8, 4, 4, 12, 12,   # 58-63
  2, 2, 6, 4, 12, 12,   # 64-69
  2, 2, 2, 6, 12, 12,   # 70-75
  10, 10, 10, 10, 14, 12, # 76-81
  10, 10, 10, 10, 10, 14, # 82-87
  1, 5, 4, 4, 12, 12,   # 88-93
  3, 3, 8, 4, 12, 12,   # 94-99
  2, 2, 2, 6, 12, 12,   # 100-105
  2, 2, 2, 2, 6, 12,    # 106-111
  10, 10, 10, 10, 14, 12, # 112-117
  10, 10, 10, 10, 10, 14, # 118-123
  1, 5, 5, 4, 12, 12,   # 124-129
  3, 3, 8, 4, 12, 12,   # 130-135
  3, 3, 3, 8, 12, 12,   # 136-141
  2, 2, 2, 2, 6, 12,    # 142-147
  10, 10, 10, 10, 14, 12, # 148-153
  10, 10, 10, 10, 10, 14, # 154-159
  9, 5, 5, 5, 13, 12,   # 160-165
  3, 3, 8, 8, 12, 12,   # 166-171
  3, 3, 3, 8, 12, 12,   # 172-177
  3, 3, 3, 3, 8, 12,    # 178-183
  11, 11, 10, 10, 14, 12, # 184-189
  10, 10, 10, 10, 10, 14, # 190-195
  9, 9, 5, 5, 13, 12,   # 196-201
  9, 9, 9, 13, 13, 12,  # 202-207
  3, 3, 3, 8, 8, 12,    # 208-213
  3, 3, 3, 3, 8, 14,    # 214-219
  11, 11, 11, 11, 7, 12, # 220-225
  11, 11, 11, 11, 11, 15, # 226-231
  0, 0, 0, 0, 0, 0,     # 232-237 (grayscale dark)
  8, 8, 8, 8, 8, 8,     # 238-243
  7, 7, 7, 7, 7, 7,     # 244-249
  15, 15, 15, 15, 15, 15, # 250-255 (grayscale light)
].freeze

Class Method Summary collapse

Class Method Details

.detectObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chamomile/styling/color_profile.rb', line 56

def detect
  return NO_COLOR if ENV.key?("NO_COLOR")

  colorterm = ENV.fetch("COLORTERM", "")
  return TRUE_COLOR if %w[truecolor 24bit].include?(colorterm)

  term = ENV.fetch("TERM", "")
  return ANSI256 if term.include?("256color")
  return ANSI if term.include?("color") || term.include?("ansi")

  ANSI
end

.downsample(color, target_profile) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chamomile/styling/color_profile.rb', line 69

def downsample(color, target_profile)
  return Color::NoColor.new if target_profile == NO_COLOR

  case color
  when Color::TrueColor
    case target_profile
    when TRUE_COLOR then color
    when ANSI256 then truecolor_to_256(color)
    when ANSI then ansi256_to_ansi(truecolor_to_256(color))
    end
  when Color::ANSI256Color
    case target_profile
    when TRUE_COLOR, ANSI256 then color
    when ANSI then ansi256_to_ansi(color)
    end
  else # ANSIColor, NoColor, etc.
    color
  end
end