Class: ScaleFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/admiral-tools-figma/helper/figma/figma_image_downloader/helpers/scale_formatter.rb

Instance Method Summary collapse

Instance Method Details

#any_scales_to_scales(scales) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/admiral-tools-figma/helper/figma/figma_image_downloader/helpers/scale_formatter.rb', line 16

def any_scales_to_scales(scales)
  return [] if scales.nil?

  result_scales = []
  scales.each do |scale|
    case scale
    when String
      converted_scale = scale_from_dpi(scale)
      if !converted_scale.nil?
        result_scales.push(converted_scale)
      else
        float_scale = scale.to_f
        result_scales.push(float_scale) unless float_scale.nil?
      end
    when Numeric
      result_scales.push(scale)
    else
      next
    end
  end
  result_scales.uniq
end

#dpi_from_scale(scale) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/admiral-tools-figma/helper/figma/figma_image_downloader/helpers/scale_formatter.rb', line 59

def dpi_from_scale(scale)
  scale = scale.round(2)
  case scale
  when 0.75
    'ldpi'
  when 1.0
    'mdpi'
  when 1.5
    'hdpi'
  when 2.0
    'xhdpi'
  when 3.0
    'xxhdpi'
  when 4.0
    'xxxhdpi'
  else
    nil
  end
end

#dpis_from_scales(scales) ⇒ Object



10
11
12
13
14
# File 'lib/admiral-tools-figma/helper/figma/figma_image_downloader/helpers/scale_formatter.rb', line 10

def dpis_from_scales(scales)
  return [] if scales.nil?

  scales.map { |s| dpi_from_scale(s) }.compact
end

#formatted_scale(scale:, convert_to_dpi: false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/admiral-tools-figma/helper/figma/figma_image_downloader/helpers/scale_formatter.rb', line 79

def formatted_scale(scale:, convert_to_dpi: false)
  return nil if scale.nil?

  dpi_scale = dpi_from_scale(scale)

  if convert_to_dpi && !dpi_scale.nil?
    dpi_scale
  else
    "#{scale.to_s_formatted.gsub(/[.,]/, '_')}x"
  end
end

#scale_from_dpi(dpi) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/admiral-tools-figma/helper/figma/figma_image_downloader/helpers/scale_formatter.rb', line 39

def scale_from_dpi(dpi)
  dpi = dpi.downcase
  case dpi
  when 'ldpi'
    0.75
  when 'mdpi'
    1.0
  when 'hdpi'
    1.5
  when 'xhdpi'
    2.0
  when 'xxhdpi'
    3.0
  when 'xxxhdpi'
    4.0
  else
    nil
  end
end

#scales_from_dpis(dpis) ⇒ Object



4
5
6
7
8
# File 'lib/admiral-tools-figma/helper/figma/figma_image_downloader/helpers/scale_formatter.rb', line 4

def scales_from_dpis(dpis)
  return [] if dpis.nil?

  dpis.map { |d| scale_from_dpi(d) }.compact
end