Skip to content

Commit 4ee0f7b

Browse files
lelandrichardsonFacebook Github Bot 6
authored andcommitted
Round interpolation for RGB components
Summary:The CSS spec doesn't allow for decimal values inside of rgb colors, however the RN implementation does, so there was a disconnect here. This tests to see if the output range is an rgb color, and if so, rounds the first 3 interpolated components (but not the 4th, since that would be opacity and allows for a decimal). cc vjeux Closes facebook/react-native#6984 Differential Revision: D3186473 fb-gh-sync-id: a320bf2311764e084386700bf8c8a42ab2a347eb fbshipit-source-id: a320bf2311764e084386700bf8c8a42ab2a347eb
1 parent 94c3a04 commit 4ee0f7b

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

Libraries/Animated/src/Interpolation.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,27 @@ function createInterpolationFromStringOutputRange(
222222
});
223223
});
224224

225+
// rgba requires that the r,g,b are integers.... so we want to round them, but we *dont* want to
226+
// round the opacity (4th column).
227+
const shouldRound = isRgbOrRgba(outputRange[0]);
228+
225229
return (input) => {
226230
var i = 0;
227231
// 'rgba(0, 100, 200, 0)'
228232
// ->
229233
// 'rgba(${interpolations[0](input)}, ${interpolations[1](input)}, ...'
230234
return outputRange[0].replace(stringShapeRegex, () => {
231-
return String(interpolations[i++](input));
235+
const val = +interpolations[i++](input);
236+
const rounded = shouldRound && i < 4 ? Math.round(val) : val;
237+
return String(rounded);
232238
});
233239
};
234240
}
235241

242+
function isRgbOrRgba(range) {
243+
return typeof range === 'string' && range.startsWith('rgb');
244+
}
245+
236246
function checkPattern(arr: Array<string>) {
237247
var pattern = arr[0].replace(stringShapeRegex, '');
238248
for (var i = 1; i < arr.length; ++i) {

Libraries/Animated/src/__tests__/Interpolation-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ describe('Interpolation', () => {
231231
});
232232

233233
expect(interpolation(0)).toBe('rgba(0, 34, 68, 1)');
234-
expect(interpolation(0.5)).toBe('rgba(76.5, 110.5, 161.5, 1)');
234+
expect(interpolation(0.5)).toBe('rgba(77, 111, 162, 1)');
235235
expect(interpolation(1)).toBe('rgba(153, 187, 255, 1)');
236236
});
237237

@@ -242,7 +242,7 @@ describe('Interpolation', () => {
242242
});
243243

244244
expect(interpolation(0)).toBe('rgba(255, 149, 0, 1)');
245-
expect(interpolation(0.5)).toBe('rgba(195, 200.5, 56, 1)');
245+
expect(interpolation(0.5)).toBe('rgba(195, 201, 56, 1)');
246246
expect(interpolation(1)).toBe('rgba(135, 252, 112, 1)');
247247
});
248248

@@ -253,7 +253,7 @@ describe('Interpolation', () => {
253253
});
254254

255255
expect(interpolation(0)).toBe('rgba(100, 120, 140, 0.4)');
256-
expect(interpolation(0.5)).toBe('rgba(117.5, 186, 126, 0.7)');
256+
expect(interpolation(0.5)).toBe('rgba(118, 186, 126, 0.7)');
257257
expect(interpolation(1)).toBe('rgba(135, 252, 112, 1)');
258258
});
259259

@@ -284,7 +284,7 @@ describe('Interpolation', () => {
284284

285285
expect(interpolation(0)).toBe('rgba(0, 100, 200, 0)');
286286
expect(interpolation(0.5)).toBe('rgba(25, 125, 225, 0.5)');
287-
expect(interpolation(1.5)).toBe('rgba(152.5, 75, 125, 1)');
287+
expect(interpolation(1.5)).toBe('rgba(153, 75, 125, 1)');
288288
expect(interpolation(2)).toBe('rgba(255, 0, 0, 1)');
289289
});
290290

0 commit comments

Comments
 (0)