Harden logic for converting from CSS gradients to QGradient

Some of the gradients from https://webgradients.com/ are not minified
completely, so we need to be a bit more lenient when converting them
to the internal format used by QGradient.

Change-Id: I47466b6a77abd6d2fefc1326fbf6ba5713dd74cb
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
Tor Arne Vestbø 2018-06-27 15:40:19 +02:00
parent fc926985a1
commit 465098088e
3 changed files with 13 additions and 6 deletions

View File

@ -1393,7 +1393,7 @@ QGradient::QGradient(Preset preset)
m_data.linear.y2 = end[QLatin1Literal("y")].toDouble(); m_data.linear.y2 = end[QLatin1Literal("y")].toDouble();
for (const QJsonValue &stop : presetData[QLatin1String("stops")].toArray()) { for (const QJsonValue &stop : presetData[QLatin1String("stops")].toArray()) {
setColorAt(stop[QLatin1Literal("stop")].toDouble(), setColorAt(stop[QLatin1Literal("position")].toDouble(),
QColor(QRgb(stop[QLatin1Literal("color")].toInt()))); QColor(QRgb(stop[QLatin1Literal("color")].toInt())));
} }

View File

@ -44,7 +44,7 @@ if (argc < 3) {
} }
const filename = process.argv[argc - 1]; const filename = process.argv[argc - 1];
const mode = argc > 3 ? process.argv[argc - 2] : ''; const mode = argc > 3 ? process.argv[argc - 2] : 'json';
fs.readFile(filename, (err, css) => { fs.readFile(filename, (err, css) => {
postcss([minifyGradients]).process(css) postcss([minifyGradients]).process(css)
@ -99,15 +99,22 @@ fs.readFile(filename, (err, css) => {
const end = { x: 0.5 + x, y: 0.5 + y }; const end = { x: 0.5 + x, y: 0.5 + y };
let stops = [] let stops = []
let lastPosition = 0;
args.slice(1).forEach((arg, index) => { args.slice(1).forEach((arg, index) => {
let [color, stop = !index ? '0%' : '100%'] = arg; let [color, position = !index ? '0%' : '100%'] = arg;
stop = parseInt(stop) / 100; position = parseInt(position) / 100;
if (position < lastPosition)
position = lastPosition;
lastPosition = position;
color = parseColor(color).hex; color = parseColor(color).hex;
color = parseInt(color.slice(1), 16) color = parseInt(color.slice(1), 16)
stops.push({ color, stop }) stops.push({ color, position })
}); });
gradients[gradients.length - 1] = { start, end, stops }; gradients[gradients.length - 1] = { start, end, stops };
if (mode == 'debug')
console.log(name, args, gradients[gradients.length - 1])
}); });
enums.push(name); enums.push(name);
@ -117,7 +124,7 @@ fs.readFile(filename, (err, css) => {
}); });
// Done walking declarations // Done walking declarations
if (mode != 'enums') if (mode == 'json')
console.log(JSON.stringify(gradients, undefined, 4)); console.log(JSON.stringify(gradients, undefined, 4));
}); });
}); });