Class: DXRubyEditor::Core::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/dxruby_editor/core/theme.rb

Constant Summary collapse

DEFAULT_THEME =
{
  bg: '#333842',
  font: '#D7DAE0',
  lineno: '#636D83',
  lineno_active: '#ABB2BF',
  line_highlight: '#99BBFF0A',
  cursor: '#528BFF',
  scrollbar: '#4E566680',
  scrollbar_active: '#747D9180',

  syntax_comment: '#5C6370',
  syntax_class: '#E5C07B',
  syntax_keyword: '#C678DD',
  syntax_op: '#C678DD',
  syntax_constant: '#D19A66',
  syntax_numeric: '#D19A66',
  syntax_variable: '#E06C75',
  syntax_string: '#98C379',
  syntax_regex: '#56B6C2',
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Theme

Returns a new instance of Theme.



4
5
6
7
8
9
10
11
12
# File 'lib/dxruby_editor/core/theme.rb', line 4

def initialize(hash = {})
  @theme = DEFAULT_THEME.merge(hash).map do |tag, color|
    if self.class.typeof_color_code?(color)
      [tag, self.class.parse_color_code(color)]
    else
      [tag, color]
    end
  end.to_h
end

Class Method Details

.hex_to_int(hex) ⇒ Integer

Returns like 171, 241.

Parameters:

  • hex (String)

    like 'ab', 'f1'

Returns:

  • (Integer)

    like 171, 241



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dxruby_editor/core/theme.rb', line 64

def self.hex_to_int(hex)
  int = 0
  scale = 1
  hex.split('').reverse.each do |char|
    num = if char =~ /[a-f]/
            10 + char.ord - 'a'.ord
          else
            char.to_i
          end

    int += num * scale
    scale *= 16
  end
  int
end

.parse_color_code(color_code) ⇒ Array<Integer>

Returns like [255, 255, 255], [120, 255, 0, 0].

Parameters:

  • color_code (String)

    like '#f1f1f1', '#f00'

Returns:

  • (Array<Integer>)

    like [255, 255, 255], [120, 255, 0, 0]

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dxruby_editor/core/theme.rb', line 42

def self.parse_color_code(color_code)
  if /^#(?<hex>\h+)$/i =~ color_code
    hex.downcase!
    case hex.length
    when 3, 4
      hex = hex.split('').map { |char| char * 2 }.join
    when 6, 8
    else
      raise ArgumentError, "Misstake! color_code ('##{hex}')"
    end

    ary = []
    3.times { |i| ary << hex_to_int(hex[i * 2..i * 2 + 1]) }
    ary.unshift(hex_to_int(hex[6..7])) if hex.length == 8
    ary
  else
    raise ArgumentError, "Misstake! color_code format ('##{color_code}')"
  end
end

.typeof_color_code?(color_code) ⇒ Boolean

Parameters:

  • color_code (String)

    like '#f1f1f1', '#ff0000'

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dxruby_editor/core/theme.rb', line 25

def self.typeof_color_code?(color_code)
  if /^#(?<hex>\h+)$/i =~ color_code
    if hex.length == 3 ||
       hex.length == 4 ||
       hex.length == 6 ||
       hex.length == 8
      return true
    end

    raise ArgumentError, "Misstake! color_code ('##{hex}')"
  end
  false
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/dxruby_editor/core/theme.rb', line 18

def [](key)
  @theme[key]
end

#[]=(key, value) ⇒ Object



14
15
16
# File 'lib/dxruby_editor/core/theme.rb', line 14

def []=(key, value)
  @theme[key] = value
end