smart_ass 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,4 @@ class SmartAss::BT709ColorMatrix < SmartAss::YCbCrColorMatrix
2
2
  def initialize
3
3
  super(0.2126, 0.7152, 0.0722)
4
4
  end
5
-
6
- def offset_vector
7
- Matrix.column_vector([22.0, 125.0, 125.0])
8
- end
9
5
  end
@@ -2,15 +2,25 @@ class SmartAss::RGBAColor
2
2
  attr_reader :r, :g, :b, :a
3
3
 
4
4
  def self.from_ass(string)
5
- argb_hex = string.gsub(/^&H/, '')
6
- components = argb_hex.scan(/.{2}/).map {|h| h.to_i(16)}
7
- if components.size == 4
8
- from_argb(*components)
5
+ abgr_hex = string.gsub(/^&H/, '')
6
+ components = abgr_hex.scan(/.{2}/).map {|h| h.to_i(16)}
7
+
8
+ if components.size >= 4
9
+ from_abgr(*components)
9
10
  else
10
- from_rgba(*components)
11
+ from_bgr(*components)
11
12
  end
12
13
  end
13
14
 
15
+ def self.from_abgr(*components)
16
+ argb = [components[0], components[1..3].reverse].flatten
17
+ from_argb(*argb)
18
+ end
19
+
20
+ def self.from_bgr(*components)
21
+ from_rgba(*components.reverse)
22
+ end
23
+
14
24
  def self.from_argb(*components)
15
25
  from_rgba(*components.push(components.shift))
16
26
  end
@@ -20,7 +30,7 @@ class SmartAss::RGBAColor
20
30
  end
21
31
 
22
32
  def to_ass
23
- hex = argb_components
33
+ hex = abgr_components
24
34
  .map {|c| c.to_s(16)}
25
35
  .map {|c| c.rjust(2, "0")}
26
36
  .map(&:upcase)
@@ -41,6 +51,11 @@ class SmartAss::RGBAColor
41
51
  c.unshift(c.pop)
42
52
  end
43
53
 
54
+ def abgr_components
55
+ argb = argb_components
56
+ [argb[0], argb[1..3].reverse].flatten
57
+ end
58
+
44
59
  def rgb_components
45
60
  c = components
46
61
  c.pop
@@ -1,3 +1,3 @@
1
1
  module SmartAss
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -22,7 +22,7 @@ class SmartAss::YCbCrColorMatrix
22
22
 
23
23
  # YPbPr -> RGB []
24
24
  def to_rgb_matrix_from_ypbpr
25
- zeros to_ypbpr_matrix_from_rgb.inverse
25
+ to_ypbpr_matrix_from_rgb.inverse
26
26
  end
27
27
 
28
28
  # RGB -> YCbCr []
@@ -32,32 +32,32 @@ class SmartAss::YCbCrColorMatrix
32
32
 
33
33
  # YCbCr -> RGB []
34
34
  def to_rgb_matrix_from_ycbcr
35
- zeros to_ycbcr_matrix_from_rgb.inverse
35
+ to_ycbcr_matrix_from_rgb.inverse
36
36
  end
37
37
 
38
38
  # RGB(digital) -> YCbCr []
39
39
  def to_ycbcr_matrix_from_rgbd
40
- scale_rows to_ycbcr_matrix_from_rgb, *[256.0/255.0]*3
40
+ scale_rows to_ycbcr_matrix_from_rgb, *[1.0/255.0]*3
41
41
  end
42
42
 
43
43
  # YCbCr -> RGB(digital) []
44
44
  def to_rgbd_matrix_from_ycbcr
45
- scale_rows zeros(to_ycbcr_matrix_from_rgbd.inverse), *[256.0*256.0]*3
45
+ to_ycbcr_matrix_from_rgbd.inverse
46
46
  end
47
47
 
48
48
  # RGB(digital) -> YCbCr Equation
49
49
  def to_ycbcr_from_rgb(*rgb)
50
- apply_equation(*rgb) do |*rgb|
51
- offset_vector + (to_ycbcr_matrix_from_rgbd * 1.0 / 256.0) *
52
- Matrix.column_vector(rgb)
50
+ r = apply_equation(*rgb) do |*rgb|
51
+ offset_vector + (to_ycbcr_matrix_from_rgbd * Matrix.column_vector(rgb))
53
52
  end
53
+
54
+ clip_ycbcr(r)
54
55
  end
55
56
 
56
57
  # YCbCr -> RGB(digital) Equation
57
58
  def to_rgb_from_ycbcr(*ycbcr)
58
59
  r = apply_equation(*ycbcr) do |*ycbcr|
59
- (to_rgbd_matrix_from_ycbcr * 1.0 / 256.0) *
60
- (Matrix.column_vector(ycbcr) - offset_vector)
60
+ to_rgbd_matrix_from_ycbcr * (Matrix.column_vector(ycbcr) - offset_vector)
61
61
  end.map(&:round)
62
62
 
63
63
  clip_rgb(r)
@@ -65,8 +65,33 @@ class SmartAss::YCbCrColorMatrix
65
65
 
66
66
  private
67
67
  def clip_rgb(input)
68
- input.map {|n| n >= 0 ? n : 0}
69
- .map {|n| n <= 255 ? n : 255}
68
+ input.map {|n| clip_component(n, 0, 255)}
69
+ end
70
+
71
+ def clip_ycbcr(input)
72
+ [
73
+ clip_component(input[0], *y_minmax),
74
+ clip_component(input[1], *cbcr_minmax),
75
+ clip_component(input[2], *cbcr_minmax)
76
+ ]
77
+ end
78
+
79
+ def clip_component(c, min, max)
80
+ if c < min
81
+ min
82
+ elsif c > max
83
+ max
84
+ else
85
+ c
86
+ end
87
+ end
88
+
89
+ def y_minmax
90
+ [16.0, 235.0]
91
+ end
92
+
93
+ def cbcr_minmax
94
+ [16.0, 240.0]
70
95
  end
71
96
 
72
97
  def offset_vector
@@ -83,14 +108,4 @@ class SmartAss::YCbCrColorMatrix
83
108
  matrix.row(index) * factor
84
109
  }]
85
110
  end
86
-
87
- def zeros(matrix)
88
- matrix.map {|n|
89
- if n < 1e-5 and n > -1e-5 then
90
- 0.0
91
- else
92
- n
93
- end
94
- }
95
- end
96
111
  end
@@ -27,14 +27,14 @@ describe SmartAss::RGBAColor do
27
27
  end
28
28
 
29
29
  it "can be created from an .ass hex (without alpha)" do
30
- c = SmartAss::RGBAColor.from_ass("&HFFFFFF")
31
- c.components.should == [255, 255, 255, 0]
30
+ c = SmartAss::RGBAColor.from_ass("&H1122AAFF")
31
+ c.components.should == [255, 0xAA, 0x22, 0x11]
32
32
  end
33
33
 
34
34
  describe "#to_ass" do
35
35
  it "returns ass repesentation" do
36
- c = SmartAss::RGBAColor.from_argb(0x00, 0xff, 0xff, 0xff)
37
- c.to_ass.should == "&H00FFFFFF"
36
+ c = SmartAss::RGBAColor.from_argb(0x00, 0xff, 0x00, 0x00)
37
+ c.to_ass.should == "&H000000FF"
38
38
 
39
39
  c = SmartAss::RGBAColor.from_rgba(*[0xff, 0xff, 0xff] + [0x00])
40
40
  c.to_ass.should == "&H00FFFFFF"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_ass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-17 00:00:00.000000000 Z
12
+ date: 2012-11-06 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Utility for YCbCr ColorMatrix conversion of ASS Subtitles.
15
15
  email:
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  version: '0'
64
64
  requirements: []
65
65
  rubyforge_project:
66
- rubygems_version: 1.8.24
66
+ rubygems_version: 1.8.23
67
67
  signing_key:
68
68
  specification_version: 3
69
69
  summary: Utility for YCbCr ColorMatrix conversion of ASS Subtitles.