File: color_spec.rb

package info (click to toggle)
ruby-rghost 0.9.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,188 kB
  • sloc: ruby: 3,374; makefile: 6; sh: 1
file content (172 lines) | stat: -rw-r--r-- 5,902 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# frozen_string_literal: true

RSpec.describe RGhost::Color do
  context "color factory" do
    # String HTML color converter
    # Color.create '#FFAA33'
    # As Symbol will be find in RGhost::Constants::Colors::RGB
    # Color.create :red
    # As Array with 3 elements
    # Color.create [0.5, 0.3, 0.5]
    # Hash with 3 pair of key/value. Valids keys :red, :green and :blue
    # Color.create :red => 0.5, :green => 0.3, :blue => 0.5
    # Hash with 3 pair of key/value. Valids keys :r, :g and :b
    # Color.create :r => 0.5, :g => 0.3, :b => 0.5
    #====Creating CMYK color
    # Hash with 4 pair of key/value. Valids keys :cyan, :magenta, :yellow and :black
    #  Color.create :cyan=> 1 ,:magenta => 0.3, :yellow => 0, :black => 0
    # Hash with 4 pair of key/value. Valids keys :c, :m, :y and :b
    #  Color.create :c=> 1 ,:m => 0.3, :y => 0, :b => 0
    #====Creating CMYK Spot color
    # Hash with 5 pair of key/value. Valids keys :cyan, :magenta, :yellow, :black, and :name
    #  Color.create :cyan=> 0, :magenta => 100, :yellow => 63, :black => 12, :name => 'Pantone 200 C'
    #====Creating Gray color
    # A single Numeric
    # Color.create 0.5
    # 50 percent of black will be divided by 100.0
    # Color.create 50

    it "should create RGB color for hex color(html style)" do
      c = RGhost::Color.create "#FF00FF"
      c.instance_of?(RGhost::RGB)
      expect(c.ps.to_s).to eq("1.0 0.0 1.0 setrgbcolor")
    end

    it "should create RGB color by color name" do
      c = RGhost::Color.create :red
      c.instance_of?(RGhost::RGB)
      expect(c.ps.to_s).to eq("1.0 0.0 0.0 setrgbcolor")
    end

    it "should create RGB color by array, 3 elements" do
      c = RGhost::Color.create [1, 0, 1]
      c.instance_of?(RGhost::RGB)
      expect(c.ps.to_s).to eq("1 0 1 setrgbcolor")
    end

    it "should create color with hash and RGB color names" do
      c = RGhost::Color.create red: 0.5, green: 0.7, blue: 1
      c.instance_of?(RGhost::RGB)
      expect(c.ps.to_s).to eq("0.5 0.7 1 setrgbcolor")
    end

    it "should create color with hash and RGB short color names " do
      c = RGhost::RGB.new r: 0.5, g: 0.7, b: 1
      c.instance_of?(RGhost::RGB)
      expect(c.ps.to_s).to eq("0.5 0.7 1 setrgbcolor")
    end

    it "should create a color by hash" do
      c = RGhost::Color.create cyan: 1, magenta: 0.3, yellow: 0, black: 1
      c.instance_of?(RGhost::CMYK)
      expect(c.ps.to_s).to eq("1 0.3 0 1 setcmykcolor")
    end

    it "should create a color by hash with short names" do
      c = RGhost::Color.create c: 1, m: 0.3, y: 0, k: 1
      c.instance_of?(RGhost::CMYK)
      expect(c.ps.to_s).to eq("1 0.3 0 1 setcmykcolor")
    end

    it "should create a color by hash with short names" do
      c = RGhost::Color.create [1, 0.3, 0, 1]
      c.instance_of?(RGhost::CMYK)
      expect(c.ps.to_s).to eq("1 0.3 0 1 setcmykcolor")
    end

    it "should create a color by a numeric" do
      c = RGhost::Color.create 0.5
      c.instance_of?(RGhost::Gray)
      expect(c.ps.to_s).to eq("0.5 setgray")
    end
  end

  context RGhost::Gray do
    it "should create gray scale" do
      c = RGhost::Gray.new 1
      expect(c.ps.to_s).to eq("1 setgray")

      c = RGhost::Gray.new 0.5
      expect(c.ps.to_s).to eq("0.5 setgray")
    end

    it "should create gray scale using percentage" do
      c = RGhost::Gray.new 100
      expect(c.ps.to_s).to eq("1.0 setgray")

      c = RGhost::Gray.new 50
      expect(c.ps.to_s).to eq("0.5 setgray")
    end
  end

  context RGhost::CMYK do
    it "should create a color using default values" do
      c = RGhost::CMYK.new
      expect(c.ps.to_s).to eq("1 0 0 0 setcmykcolor")
    end

    it "should create a color by hash" do
      c = RGhost::CMYK.new cyan: 1, magenta: 0.3, yellow: 0, black: 1
      expect(c.ps.to_s).to eq("1 0.3 0 1 setcmykcolor")
    end

    it "should create a color by hash with short names" do
      c = RGhost::CMYK.new c: 1, m: 0.3, y: 0, k: 1
      expect(c.ps.to_s).to eq("1 0.3 0 1 setcmykcolor")
    end

    it "should create a color by array with 4 elements" do
      c = RGhost::CMYK.new [1, 0.3, 0, 1]
      expect(c.ps.to_s).to eq("1 0.3 0 1 setcmykcolor")
    end
  end

  context RGhost::RGB do
    it "should create a color from a hex(html style)" do
      c = RGhost::RGB.new "#ABBACA"
      expect(c.ps.to_s).to eq("0.6705882352941176 0.7294117647058823 0.792156862745098 setrgbcolor")

      c = RGhost::RGB.new "#FFFFFF"
      expect(c.ps.to_s).to eq("1.0 1.0 1.0 setrgbcolor")

      c = RGhost::RGB.new "#000000"
      expect(c.ps.to_s).to eq("0.0 0.0 0.0 setrgbcolor")

      c = RGhost::RGB.new "#FF0000"
      expect(c.ps.to_s).to eq("1.0 0.0 0.0 setrgbcolor")

      c = RGhost::RGB.new "#FFFF00"
      expect(c.ps.to_s).to eq("1.0 1.0 0.0 setrgbcolor")

      c = RGhost::RGB.new "#334455"
      expect(c.ps.to_s).to eq("0.2 0.26666666666666666 0.3333333333333333 setrgbcolor")
    end

    # it "should create a color from a name defined at RGhost::Constants::Colors::RGB" do
    it "should create a color from a array with 3 elements" do
      c = RGhost::RGB.new [0, 0, 0]
      expect(c.ps.to_s).to eq("0 0 0 setrgbcolor")

      c = RGhost::RGB.new [1, 1, 1]
      expect(c.ps.to_s).to eq("1 1 1 setrgbcolor")

      c = RGhost::RGB.new [0.5, 0.5, 0.5]
      expect(c.ps.to_s).to eq("0.5 0.5 0.5 setrgbcolor")
    end

    it "should truncate values greater than 1" do
      c = RGhost::RGB.new [2, 2, 2]
      expect(c.ps.to_s).to eq("0.02 0.02 0.02 setrgbcolor")
    end

    it "should create color with hash and RGB color names" do
      c = RGhost::RGB.new red: 0.5, green: 0.7, blue: 1
      expect(c.ps.to_s).to eq("0.5 0.7 1 setrgbcolor")
    end

    it "should create color with hash and RGB short color names " do
      c = RGhost::RGB.new r: 0.5, g: 0.7, b: 1
      expect(c.ps.to_s).to eq("0.5 0.7 1 setrgbcolor")
    end
  end
end