Class: SportsManager::SolutionDrawer::Mermaid::NodeStyle

Inherits:
Object
  • Object
show all
Defined in:
lib/sports_manager/solution_drawer/mermaid/node_style.rb

Constant Summary collapse

COLORS =

TODO: Add an algorithms to dynamically create the colors

%w[
  #A9F9A9 #4FF7DE #FF6E41
  #5FAD4E #009BFF #00FF00
  #FF0000 #01FFFE #FFA6FE
  #FFDB66 #006401 #010067
  #95003A #007DB5 #FF00F6
  #FFEEE8 #774D00 #90FB92
  #0076FF #D5FF00 #FF937E
  #6A826C #FF029D #FE8900
  #7A4782 #7E2DD2 #85A900
  #FF0056 #A42400 #00AE7E
  #683D3B #BDC6FF #263400
  #BDD393 #00B917 #9E008E
  #001544 #C28C9F #FF74A3
  #01D0FF #004754 #E56FFE
  #788231 #0E4CA1 #91D0CB
  #BE9970 #968AE8 #BB8800
  #43002C #DEFF74 #00FFC6
  #FFE502 #620E00 #008F9C
  #98FF52 #7544B1 #B500FF
  #00FF78 #005F39 #6B6882
  #A75740 #A5FFD2 #FFB167
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ NodeStyle

Returns a new instance of NodeStyle.



38
39
40
# File 'lib/sports_manager/solution_drawer/mermaid/node_style.rb', line 38

def initialize(elements)
  @elements = elements.map(&:to_s)
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



32
33
34
# File 'lib/sports_manager/solution_drawer/mermaid/node_style.rb', line 32

def elements
  @elements
end

Class Method Details

.call(elements) ⇒ Object



34
35
36
# File 'lib/sports_manager/solution_drawer/mermaid/node_style.rb', line 34

def self.call(elements)
  new(elements).to_params
end

Instance Method Details

#class_definitionsObject



55
56
57
58
59
60
61
62
# File 'lib/sports_manager/solution_drawer/mermaid/node_style.rb', line 55

def class_definitions
  colorscheme.map do |element, properties|
    attributes = properties
      .map { |property| property.join(':') }
      .join(', ')
    "#{element} #{attributes}"
  end
end

#colorschemeObject



64
65
66
67
68
69
70
71
# File 'lib/sports_manager/solution_drawer/mermaid/node_style.rb', line 64

def colorscheme
  elements.each_with_index.with_object({}) do |(element, index), scheme|
    node_color = COLORS[index]
    text_color = text_color(node_color)

    scheme[element] = { fill: node_color, color: text_color }
  end
end

#subgraph_colorschemeObject



46
47
48
49
50
51
52
53
# File 'lib/sports_manager/solution_drawer/mermaid/node_style.rb', line 46

def subgraph_colorscheme
  elements.map do |element|
    node_name = element.upcase
    style_class = element

    "#{node_name}:::#{style_class}"
  end
end

#text_color(color) ⇒ Object

Internal: ripoff from stackoverflow



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sports_manager/solution_drawer/mermaid/node_style.rb', line 74

def text_color(color)
  red, green, blue = color
    .gsub('#', '')
    .chars
    .each_slice(2)
    .map(&:join)
    .map(&:hex)

  formula = (red * 0.299) + (green * 0.587) + (blue * 0.114)

  formula > 186 ? '#000000' : '#FFFFFF'
end

#to_paramsObject



42
43
44
# File 'lib/sports_manager/solution_drawer/mermaid/node_style.rb', line 42

def to_params
  { class_definitions: class_definitions, subgraph_colorscheme: subgraph_colorscheme }
end