Debian Codegraph @

texmf-dist/doc/latex/spotxcolor/tools/acb2def.rb

raw
1 #!/usr/bin/env ruby
2 # frozen_string_literal: true
3
4 # acb2def.rb
5 # Parses Adobe Color Book (.acb) files and generates spotxcolor .def files.
6 # Uses a 3rd-degree Polynomial Regression model trained on 1280 native
7 # Adobe DIC color pairs to accurately predict CMYK from Lab, strictly
8 # eliminating K-channel muddiness for vibrant spot colors.
9
10 if ARGV.empty?
11 puts "Usage: ruby acb2def.rb <input.acb> [output.def]"
12 exit 1
13 end
14
15 input_file = ARGV[0]
16 output_file = ARGV[1] || input_file.sub(/\.acb$/i, '.def')
17
18 def read_acb_string(f)
19 len = f.read(4).unpack1('N')
20 return "" if len == 0
21 bytes = f.read(len * 2)
22 raw_string = bytes.force_encoding('UTF-16BE').encode('UTF-8').sub(/\x00$/, '')
23 # "key=value" 形式の場合は value のみを抽出
24 raw_string.include?('=') ? raw_string.split('=', 2)[1] : raw_string
25 end
26
27 # 3rd-Degree Polynomial Regression Model trained on native .acbl data
28 def lab_to_cmyk_ml(l, a, b)
29 # Normalize inputs (L: 0~100 -> 0~1, a/b: -128~127 -> -1~1)
30 nl = l / 100.0
31 na = a / 128.0
32 nb = b / 128.0
33
34 # 20 Polynomial Features (Degree 3)
35 features = [
36 1.0, # Bias
37 nl, na, nb,
38 nl**2, nl*na, nl*nb, na**2, na*nb, nb**2,
39 nl**3, nl**2 * na, nl**2 * nb, nl * na**2, nl * na * nb, nl * nb**2, na**3, na**2 * nb, na * nb**2, nb**3
40 ]
41
42 # Pre-calculated weights from scikit-learn Ridge Regression on DIC Color Guide
43 weights_c = [1.2901, -0.4479, -0.3755, -0.9451, -2.3973, -2.9976, 1.7545, -2.2456, -1.3208, -0.8578, 1.5398, 2.6999, -1.2268, 3.5385, 2.0701, 2.1257, 1.1315, 0.7623, 0.6077, -1.0463]
44 weights_m = [1.3721, -1.2394, 0.2517, -0.1009, -1.2374, 3.6443, 0.7975, -0.6841, 0.3884, -1.4627, 1.0866, -3.6129, -0.5822, 1.2445, -0.6730, 1.6401, -1.1761, 0.3443, 0.7518, -0.3596]
45 weights_y = [1.5667, -2.1058, -1.0067, 2.7018, 0.0076, 1.1679, 0.9149, -1.2303, 3.3409, -3.7865, 0.4668, -0.7358, -3.0462, 2.2590, -5.5650, 8.1909, -0.1768, -1.8255, 4.8637, -4.7405]
46 weights_k = [0.8222, -3.8856, 0.1354, 0.6670, 5.9819, -0.6177, -2.0387, 0.3529, 0.9857, 0.4917, -2.9774, 0.5719, 1.4967, -0.6327, -1.5848, -0.7946, 0.1007, 0.0145, 0.3438, 0.2524]
47
48 # Calculate dot product
49 c = features.zip(weights_c).map { |x, w| x * w }.sum
50 m = features.zip(weights_m).map { |x, w| x * w }.sum
51 y = features.zip(weights_y).map { |x, w| x * w }.sum
52 k = features.zip(weights_k).map { |x, w| x * w }.sum
53
54 # Clip and Clean (Remove Muddiness)
55 c, m, y, k = [c, m, y, k].map { |v| [[v, 0.0].max, 1.0].min }
56
57 # Strict thresholding to preserve spot color vibrancy (snap near-zero values to 0)
58 c = 0.0 if c < 0.03
59 m = 0.0 if m < 0.03
60 y = 0.0 if y < 0.03
61 k = 0.0 if k < 0.05 # Aggressive K-removal
62
63 [c, m, y, k].map { |v| v.round(4) }
64 end
65
66 begin
67 File.open(input_file, 'rb') do |f|
68 signature = f.read(4)
69 raise "Invalid signature '#{signature}'." unless signature == '8BCB'
70
71 version = f.read(2).unpack1('n')
72 raise "Unsupported ACB version: #{version}" unless version == 1
73
74 f.read(2) # id
75
76 book_title = read_acb_string(f)
77 book_prefix = read_acb_string(f)
78 book_postfix = read_acb_string(f)
79 book_desc = read_acb_string(f)
80
81 color_count = f.read(2).unpack1('n')
82 f.read(4) # page_size, page_offset
83 color_space = f.read(2).unpack1('n')
84
85 channel_count = case color_space
86 when 2 then 4 # CMYK
87 when 0, 1, 7 then 3 # RGB, HSB, Lab
88 when 8 then 1 # Grayscale
89 else raise "Unknown color space ID: #{color_space}"
90 end
91
92 colors = []
93 color_count.times do
94 name = read_acb_string(f)
95 f.read(6) # code
96 channels = f.read(channel_count).unpack("C#{channel_count}")
97
98 full_pdf_name = "#{book_prefix}#{name}#{book_postfix}"
99 macro_name = full_pdf_name.gsub(/[\s\-\*\/]/, '')
100
101 if color_space == 2 # CMYK
102 c, m, y, k = channels.map { |v| (v / 255.0).round(4) }
103 val_str = "#{c}, #{m}, #{y}, #{k}"
104 elsif color_space == 7 # Lab
105 # Convert 0-255 to standard Lab ranges (L: 0-100, a/b: -128-127)
106 l_val = (channels[0] / 255.0) * 100.0
107 a_val = channels[1] - 128.0
108 b_val = channels[2] - 128.0
109
110 c, m, y, k = lab_to_cmyk_ml(l_val, a_val, b_val)
111 val_str = "#{c}, #{m}, #{y}, #{k}"
112 else
113 val_str = "0.0, 0.0, 0.0, 1.0" # Fallback
114 end
115
116 colors << { macro: macro_name, pdf_name: full_pdf_name, values: val_str }
117 end
118
119 File.open(output_file, 'w') do |out|
120 out.puts "% spotxcolor definition file for #{book_title}"
121 out.puts "% Auto-generated from .acb"
122 if color_space == 7
123 out.puts "% NOTE: Applied a 3rd-Degree Polynomial Regression Model trained on native"
124 out.puts "% Adobe .acbl data to predict highly accurate CMYK fallback values."
125 end
126 out.puts ""
127 out.puts "\\ProvidesFile{#{File.basename(output_file)}}[Spot color definitions]"
128 out.puts ""
129
130 colors.each do |c|
131 out.puts "\\definespotcolor{#{c[:macro]}}{#{c[:pdf_name]}}{#{c[:values]}}"
132 end
133 out.puts "\\endinput"
134 end
135
136 puts "Successfully extracted #{color_count} colors."
137 if color_space == 7
138 puts "Applied ML-Trained Regression to eliminate K-channel muddiness and ensure high accuracy."
139 end
140 puts "Output saved to: #{output_file}"
141 end
142 rescue => e
143 puts "Error processing file: #{e.class} - #{e.message}"
144 end