labels_helper.rb 1.3 KB
Newer Older
1
module LabelsHelper
2
  def project_label_names
3
    @project.labels.pluck(:title)
4 5
  end

6
  def render_colored_label(label)
D
Dmitriy Zaporozhets 已提交
7
    label_color = label.color || Label::DEFAULT_COLOR
8
    text_color = text_color_for_bg(label_color)
9

R
Robert Speicher 已提交
10 11 12 13 14 15 16
    # Intentionally not using content_tag here so that this method can be called
    # by LabelReferenceFilter
    span = %(<span class="label color-label") +
      %( style="background-color: #{label_color}; color: #{text_color}">) +
      label.name + '</span>'

    span.html_safe
17
  end
18 19 20

  def suggested_colors
    [
P
phortx 已提交
21
      '#0033CC',
22
      '#428BCA',
P
phortx 已提交
23 24
      '#44AD8E',
      '#A8D695',
25
      '#5CB85C',
P
phortx 已提交
26 27
      '#69D100',
      '#004E00',
28 29
      '#34495E',
      '#7F8C8D',
P
phortx 已提交
30 31
      '#A295D6',
      '#5843AD',
32
      '#8E44AD',
P
phortx 已提交
33
      '#FFECDB',
34 35 36 37 38 39 40 41
      '#AD4363',
      '#D10069',
      '#CC0033',
      '#FF0000',
      '#D9534F',
      '#D1D100',
      '#F0AD4E',
      '#AD8D43'
42 43
    ]
  end
44 45 46 47 48

  def text_color_for_bg(bg_color)
    r, g, b = bg_color.slice(1,7).scan(/.{2}/).map(&:hex)

    if (r + g + b) > 500
R
Robert Speicher 已提交
49
      '#333333'
50
    else
R
Robert Speicher 已提交
51
      '#FFFFFF'
52 53
    end
  end
54 55

  def project_labels_options(project)
56
    options_from_collection_for_select(project.labels, 'name', 'name', params[:label_name])
57
  end
R
Robert Speicher 已提交
58 59

  module_function :render_colored_label, :text_color_for_bg
60
end