Skip to content
Snippets Groups Projects
Commit 737fbb9f authored by jojohoch's avatar jojohoch
Browse files

[player] Specify the colors for marking in hexadecimal notation

The marking service takes over the conversion to rgb notation, because
in html markup this notation is used for the background color
parent dc442be3
No related branches found
No related tags found
No related merge requests found
...@@ -10,22 +10,22 @@ import { TextElement } from '../../ui-elements/text/text-element'; ...@@ -10,22 +10,22 @@ import { TextElement } from '../../ui-elements/text/text-element';
<button *ngIf="elementModel.highlightableYellow" <button *ngIf="elementModel.highlightableYellow"
type="button" type="button"
class="marking-button" class="marking-button"
mat-mini-fab [style.background-color]="'yellow'" mat-mini-fab [style.background-color]="'#f9f871'"
(click)="applySelection.emit({ mode: 'mark', color:'yellow', element })"> (click)="applySelection.emit({ mode: 'mark', color:'#f9f871', element })">
<mat-icon>border_color</mat-icon> <mat-icon>border_color</mat-icon>
</button> </button>
<button *ngIf="elementModel.highlightableTurquoise" <button *ngIf="elementModel.highlightableTurquoise"
type="button" type="button"
class="marking-button" class="marking-button"
mat-mini-fab [style.background-color]="'turquoise'" mat-mini-fab [style.background-color]="'#9de8eb'"
(click)="applySelection.emit({ mode: 'mark', color: 'turquoise', element })"> (click)="applySelection.emit({ mode: 'mark', color: '#9de8eb', element })">
<mat-icon>border_color</mat-icon> <mat-icon>border_color</mat-icon>
</button> </button>
<button *ngIf="elementModel.highlightableOrange" <button *ngIf="elementModel.highlightableOrange"
type="button" type="button"
class="marking-button" class="marking-button"
mat-mini-fab [style.background-color]="'orange'" mat-mini-fab [style.background-color]="'#ffa06a'"
(click)="applySelection.emit({ mode: 'mark', color: 'orange', element })"> (click)="applySelection.emit({ mode: 'mark', color: '#ffa06a', element })">
<mat-icon>border_color</mat-icon> <mat-icon>border_color</mat-icon>
</button> </button>
<button type="button" <button type="button"
......
...@@ -31,7 +31,8 @@ export class MarkingService { ...@@ -31,7 +31,8 @@ export class MarkingService {
} }
getMarkingData = (htmlText: string): string[] => { getMarkingData = (htmlText: string): string[] => {
const markingStartPattern = new RegExp(`<${MarkingService.MARKING_TAG.toLowerCase()} [a-z]+="[\\w- ;:]+">`); const markingStartPattern =
new RegExp(`<${MarkingService.MARKING_TAG.toLowerCase()} [a-z]+="[\\w\\d()-;:, #]+">`);
const markingClosingTag = `</${MarkingService.MARKING_TAG.toLowerCase()}>`; const markingClosingTag = `</${MarkingService.MARKING_TAG.toLowerCase()}>`;
let newHtmlText = htmlText; let newHtmlText = htmlText;
const markCollection = []; const markCollection = [];
...@@ -43,7 +44,8 @@ export class MarkingService { ...@@ -43,7 +44,8 @@ export class MarkingService {
matchesArray = newHtmlText.match(markingClosingTag); matchesArray = newHtmlText.match(markingClosingTag);
if (matchesArray) { if (matchesArray) {
const endMatch = matchesArray[0]; const endMatch = matchesArray[0];
const startIndex = newHtmlText.search(startMatch); // we have to escape the brackets of rgb color
const startIndex = newHtmlText.search(startMatch.replace('(', '\\(').replace(')', '\\)'));
newHtmlText = newHtmlText.replace(startMatch, ''); newHtmlText = newHtmlText.replace(startMatch, '');
const endIndex = newHtmlText.search(endMatch); const endIndex = newHtmlText.search(endMatch);
newHtmlText = newHtmlText.replace(endMatch, ''); newHtmlText = newHtmlText.replace(endMatch, '');
...@@ -75,12 +77,16 @@ export class MarkingService { ...@@ -75,12 +77,16 @@ export class MarkingService {
} }
private getMarkingColor = (tag: string): string => { private getMarkingColor = (tag: string): string => {
const colors = tag.match(/(?!.*:)\w*(?=;)/); const colors = tag.match(/\d{1,3}, \d{1,3}, \d{1,3}/);
return (colors) ? colors[0] : 'none'; return (colors) ? this.rgbToHex(colors[0].split(',').map(value => Number(value))) : 'none';
}; };
private createMarkingStartTag = private createMarkingStartTag(color: string): string {
(color: string): string => `<${MarkingService.MARKING_TAG.toLowerCase()} style="background-color: ${color};">`; const rgb = this.hexToRgb(color);
return `<${
MarkingService.MARKING_TAG.toLowerCase()
} style="background-color: rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]});">`;
}
private isDescendantOf(node: Node | null, element: HTMLElement): boolean { private isDescendantOf(node: Node | null, element: HTMLElement): boolean {
if (!node || node === document) { if (!node || node === document) {
...@@ -204,7 +210,9 @@ export class MarkingService { ...@@ -204,7 +210,9 @@ export class MarkingService {
start = Math.min(range.startOffset, range.endOffset); start = Math.min(range.startOffset, range.endOffset);
end = Math.max(range.startOffset, range.endOffset); end = Math.max(range.startOffset, range.endOffset);
} }
let text: string; let previousText = ''; let nextText = ''; let text: string;
let previousText = '';
let nextText = '';
if (index === 0) { if (index === 0) {
previousText = node.nodeValue?.substring(0, start) || ''; previousText = node.nodeValue?.substring(0, start) || '';
text = node.nodeValue?.substring(start) || ''; text = node.nodeValue?.substring(start) || '';
...@@ -262,4 +270,17 @@ export class MarkingService { ...@@ -262,4 +270,17 @@ export class MarkingService {
} }
}); });
} }
private rgbToHex = (rgb: number[]): string => `#${rgb.map(x => {
const hex = x.toString(16);
return hex.length === 1 ? `0${hex}` : hex;
}).join('')}`;
private hexToRgb = (hex: string): number[] => {
const normal = hex.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
if (normal) return normal.slice(1).map(e => parseInt(e, 16));
const shorthand = hex.match(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i);
if (shorthand) return shorthand.slice(1).map(e => 0x11 * parseInt(e, 16));
return [];
};
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment