Skip to content

Commit e92f177

Browse files
practualgkatsev
authored andcommitted
feat(text-track-display): Extend the constructColor function to handle 6 digit hex codes (videojs#5238)
When providing the TextTrackDisplay component with track settings overrides, currently colour codes with only three digits are supported. This updates it so that 6 digit hex codes are also accepted.
1 parent 7fd29b4 commit e92f177

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/js/tracks/text-track-display.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,30 @@ const fontMap = {
2424
* Construct an rgba color from a given hex color code.
2525
*
2626
* @param {number} color
27-
* Hex number for color, like #f0e.
27+
* Hex number for color, like #f0e or #f604e2.
2828
*
2929
* @param {number} opacity
3030
* Value for opacity, 0.0 - 1.0.
3131
*
3232
* @return {string}
3333
* The rgba color that was created, like 'rgba(255, 0, 0, 0.3)'.
34-
*
35-
* @private
3634
*/
37-
function constructColor(color, opacity) {
38-
return 'rgba(' +
35+
export function constructColor(color, opacity) {
36+
let hex;
37+
38+
if (color.length === 4) {
3939
// color looks like "#f0e"
40-
parseInt(color[1] + color[1], 16) + ',' +
41-
parseInt(color[2] + color[2], 16) + ',' +
42-
parseInt(color[3] + color[3], 16) + ',' +
40+
hex = color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
41+
} else if (color.length === 7) {
42+
// color looks like "#f604e2"
43+
hex = color.slice(1);
44+
} else {
45+
throw new Error('Invalid color code provided, ' + color + '; must be formatted as e.g. #f0e or #f604e2.');
46+
}
47+
return 'rgba(' +
48+
parseInt(hex.slice(0, 2), 16) + ',' +
49+
parseInt(hex.slice(2, 4), 16) + ',' +
50+
parseInt(hex.slice(4, 6), 16) + ',' +
4351
opacity + ')';
4452
}
4553

test/unit/tracks/text-track-display.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-env qunit */
22
import Html5 from '../../../src/js/tech/html5.js';
3+
import { constructColor } from '../../../src/js/tracks/text-track-display.js';
34
import Component from '../../../src/js/component.js';
45

56
import * as browser from '../../../src/js/utils/browser.js';
@@ -319,4 +320,33 @@ if (!Html5.supportsNativeTextTracks()) {
319320
player.dispose();
320321
});
321322

323+
QUnit.test('a color can be constructed from a three digit hex code', function(assert) {
324+
const hex = '#f0e';
325+
326+
// f gets mapped to ff -> 255 in decimal,
327+
// 0 gets mapped to 00 -> 0 in decimal,
328+
// e gets mapped to ee -> 238 in decimal.
329+
assert.equal(constructColor(hex, 1), 'rgba(255,0,238,1)');
330+
});
331+
332+
QUnit.test('a color can be constructed from a six digit hex code', function(assert) {
333+
const hex = '#f604e2';
334+
335+
// f6 -> 246 in decimal,
336+
// 04 -> 4 in decimal,
337+
// e2 -> 226 in decimal.
338+
assert.equal(constructColor(hex, 1), 'rgba(246,4,226,1)');
339+
});
340+
341+
QUnit.test('an invalid hex code will throw an error', function(assert) {
342+
const hex = '#f';
343+
344+
assert.throws(
345+
function() {
346+
constructColor(hex, 1);
347+
},
348+
new Error('Invalid color code provided, #f; must be formatted as e.g. #f0e or #f604e2.'),
349+
'colors must be valid hex codes.'
350+
);
351+
});
322352
}

0 commit comments

Comments
 (0)