Module: AsciidoctorDiagramLayout::Renderer::ColorPalette

Defined in:
lib/asciidoctor_diagram_layout/renderer/color_palette.rb

Constant Summary collapse

STROKE_COLOR =
"#d0d0d0"
GOLDEN_RATIO =
0.618033988749895
HUE_RANGE =
360

Class Method Summary collapse

Class Method Details

.hsl_to_hex(hue, saturation, lightness) ⇒ Object

Converts HSL to a hex color string, identical to Java ColorPalette.hslToHex.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/asciidoctor_diagram_layout/renderer/color_palette.rb', line 9

def self.hsl_to_hex(hue, saturation, lightness)
  s = saturation / 100.0
  l = lightness  / 100.0
  c = (1 - (2 * l - 1).abs) * s
  x = c * (1 - ((hue / 60.0) % 2 - 1).abs)
  m = l - c / 2.0
  if hue < 60
    r, g, b = c, x, 0
  elsif hue < 120
    r, g, b = x, c, 0
  elsif hue < 180
    r, g, b = 0, c, x
  elsif hue < 240
    r, g, b = 0, x, c
  elsif hue < 300
    r, g, b = x, 0, c
  else
    r, g, b = c, 0, x
  end
  ri = (r + m) * 255
  gi = (g + m) * 255
  bi = (b + m) * 255
  format("#%02x%02x%02x", ri.round, gi.round, bi.round)
end

.java_hash(str) ⇒ Object

Java String.hashCode: s*31^(n-1) + s*31^(n-2) + … + s using 32-bit signed integer overflow semantics.



36
37
38
39
40
41
42
43
44
# File 'lib/asciidoctor_diagram_layout/renderer/color_palette.rb', line 36

def self.java_hash(str)
  return 0 if str.nil? || str.empty?
  h = 0
  str.each_char do |c|
    h = (31 * h + c.ord) & 0xffffffff
  end
  # convert to signed 32-bit
  h >= 0x80000000 ? h - 0x100000000 : h
end