mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
@@ -1,8 +1,8 @@
|
||||
import { HSL } from "ngx-color"
|
||||
import { HSL } from 'ngx-color'
|
||||
|
||||
function componentToHex(c) {
|
||||
var hex = Math.floor(c).toString(16)
|
||||
return hex.length == 1 ? "0" + hex : hex
|
||||
return hex.length == 1 ? '0' + hex : hex
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -18,26 +18,26 @@ function componentToHex(c) {
|
||||
* @param Number l The lightness
|
||||
* @return Array The RGB representation
|
||||
*/
|
||||
function hslToRgb(h, s, l){
|
||||
function hslToRgb(h, s, l) {
|
||||
var r, g, b
|
||||
|
||||
if(s == 0){
|
||||
r = g = b = l // achromatic
|
||||
}else{
|
||||
function hue2rgb(p, q, t){
|
||||
if(t < 0) t += 1
|
||||
if(t > 1) t -= 1
|
||||
if(t < 1/6) return p + (q - p) * 6 * t
|
||||
if(t < 1/2) return q
|
||||
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6
|
||||
return p
|
||||
}
|
||||
if (s == 0) {
|
||||
r = g = b = l // achromatic
|
||||
} else {
|
||||
function hue2rgb(p, q, t) {
|
||||
if (t < 0) t += 1
|
||||
if (t > 1) t -= 1
|
||||
if (t < 1 / 6) return p + (q - p) * 6 * t
|
||||
if (t < 1 / 2) return q
|
||||
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
|
||||
return p
|
||||
}
|
||||
|
||||
var q = l < 0.5 ? l * (1 + s) : l + s - l * s
|
||||
var p = 2 * l - q
|
||||
r = hue2rgb(p, q, h + 1/3)
|
||||
g = hue2rgb(p, q, h)
|
||||
b = hue2rgb(p, q, h - 1/3)
|
||||
var q = l < 0.5 ? l * (1 + s) : l + s - l * s
|
||||
var p = 2 * l - q
|
||||
r = hue2rgb(p, q, h + 1 / 3)
|
||||
g = hue2rgb(p, q, h)
|
||||
b = hue2rgb(p, q, h - 1 / 3)
|
||||
}
|
||||
|
||||
return [r * 255, g * 255, b * 255]
|
||||
@@ -55,35 +55,50 @@ function hslToRgb(h, s, l){
|
||||
* @param Number b The blue color value
|
||||
* @return Array The HSL representation
|
||||
*/
|
||||
export function rgbToHsl(r, g, b){
|
||||
r /= 255, g /= 255, b /= 255;
|
||||
var max = Math.max(r, g, b), min = Math.min(r, g, b);
|
||||
var h, s, l = (max + min) / 2;
|
||||
export function rgbToHsl(r, g, b) {
|
||||
;(r /= 255), (g /= 255), (b /= 255)
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b)
|
||||
var h,
|
||||
s,
|
||||
l = (max + min) / 2
|
||||
|
||||
if(max == min){
|
||||
h = s = 0; // achromatic
|
||||
}else{
|
||||
var d = max - min;
|
||||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||
switch(max){
|
||||
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
|
||||
case g: h = (b - r) / d + 2; break;
|
||||
case b: h = (r - g) / d + 4; break;
|
||||
}
|
||||
h /= 6;
|
||||
if (max == min) {
|
||||
h = s = 0 // achromatic
|
||||
} else {
|
||||
var d = max - min
|
||||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
|
||||
switch (max) {
|
||||
case r:
|
||||
h = (g - b) / d + (g < b ? 6 : 0)
|
||||
break
|
||||
case g:
|
||||
h = (b - r) / d + 2
|
||||
break
|
||||
case b:
|
||||
h = (r - g) / d + 4
|
||||
break
|
||||
}
|
||||
h /= 6
|
||||
}
|
||||
|
||||
return [h, s, l];
|
||||
return [h, s, l]
|
||||
}
|
||||
|
||||
export function hexToHsl(hex: string): HSL {
|
||||
hex = hex.replace('#', '')
|
||||
let aRgbHex = hex.match(/.{1,2}/g)
|
||||
const hsl = rgbToHsl(parseInt(aRgbHex[0], 16), parseInt(aRgbHex[1], 16), parseInt(aRgbHex[2], 16))
|
||||
const hsl = rgbToHsl(
|
||||
parseInt(aRgbHex[0], 16),
|
||||
parseInt(aRgbHex[1], 16),
|
||||
parseInt(aRgbHex[2], 16)
|
||||
)
|
||||
return { h: hsl[0], s: hsl[1], l: hsl[2] }
|
||||
}
|
||||
|
||||
export function randomColor() {
|
||||
let rgb = hslToRgb(Math.random(), 0.6, Math.random() * 0.4 + 0.4)
|
||||
return `#${componentToHex(rgb[0])}${componentToHex(rgb[1])}${componentToHex(rgb[2])}`
|
||||
return `#${componentToHex(rgb[0])}${componentToHex(rgb[1])}${componentToHex(
|
||||
rgb[2]
|
||||
)}`
|
||||
}
|
||||
|
@@ -1,6 +1,9 @@
|
||||
import { Injectable } from "@angular/core"
|
||||
import { NgbDateParserFormatter, NgbDateStruct } from "@ng-bootstrap/ng-bootstrap"
|
||||
import { SettingsService } from "../services/settings.service"
|
||||
import { Injectable } from '@angular/core'
|
||||
import {
|
||||
NgbDateParserFormatter,
|
||||
NgbDateStruct,
|
||||
} from '@ng-bootstrap/ng-bootstrap'
|
||||
import { SettingsService } from '../services/settings.service'
|
||||
|
||||
@Injectable()
|
||||
export class LocalizedDateParserFormatter extends NgbDateParserFormatter {
|
||||
@@ -20,45 +23,71 @@ export class LocalizedDateParserFormatter extends NgbDateParserFormatter {
|
||||
*/
|
||||
private getDateParseRegex() {
|
||||
return new RegExp(
|
||||
"^" + this.getDateInputFormat()
|
||||
.replace('dd', '(?<day>[0-9]+)')
|
||||
.replace('mm', '(?<month>[0-9]+)')
|
||||
.replace('yyyy', '(?<year>[0-9]+)')
|
||||
.split('.').join('\\.\\s*') + "$" // allow whitespace(s) after dot (specific for German)
|
||||
)
|
||||
'^' +
|
||||
this.getDateInputFormat()
|
||||
.replace('dd', '(?<day>[0-9]+)')
|
||||
.replace('mm', '(?<month>[0-9]+)')
|
||||
.replace('yyyy', '(?<year>[0-9]+)')
|
||||
.split('.')
|
||||
.join('\\.\\s*') +
|
||||
'$' // allow whitespace(s) after dot (specific for German)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* This adds date separators if none are entered.
|
||||
* It also adds the current year if it wasn't entered.
|
||||
*
|
||||
* This allows users to just enter 1003, 100322, 10032022 and
|
||||
* This adds date separators if none are entered.
|
||||
* It also adds the current year if it wasn't entered.
|
||||
*
|
||||
* This allows users to just enter 1003, 100322, 10032022 and
|
||||
* have it expanded to 10.03.2022, in the case of the German format.
|
||||
* (All other formats are also supported)
|
||||
*
|
||||
*
|
||||
* It also strips any separators before running formatting and pads
|
||||
* any parts of the string, e.g. allowing for 1/2/22,
|
||||
* which allows quick entry of the date on the numpad.
|
||||
* which allows quick entry of the date on the numpad.
|
||||
*/
|
||||
private preformatDateInput(value: string): string {
|
||||
const inputFormat = this.getDateInputFormat()
|
||||
const dateSeparator = inputFormat.replace(/[dmy]/gi, '').charAt(0)
|
||||
|
||||
|
||||
if (this.separatorRegExp.test(value)) {
|
||||
// split on separator, pad & re-join without separator
|
||||
value = value.split(this.separatorRegExp).map(segment => segment.padStart(2,'0')).join('')
|
||||
}
|
||||
value = value
|
||||
.split(this.separatorRegExp)
|
||||
.map((segment) => segment.padStart(2, '0'))
|
||||
.join('')
|
||||
}
|
||||
|
||||
if (value.length == 4 && inputFormat.substring(0, 4) != 'yyyy') {
|
||||
return [value.substring(0, 2), value.substring(2, 4), new Date().getFullYear()].join(dateSeparator)
|
||||
return [
|
||||
value.substring(0, 2),
|
||||
value.substring(2, 4),
|
||||
new Date().getFullYear(),
|
||||
].join(dateSeparator)
|
||||
} else if (value.length == 4 && inputFormat.substring(0, 4) == 'yyyy') {
|
||||
return [new Date().getFullYear(), value.substring(0, 2), value.substring(2, 4)].join(dateSeparator)
|
||||
return [
|
||||
new Date().getFullYear(),
|
||||
value.substring(0, 2),
|
||||
value.substring(2, 4),
|
||||
].join(dateSeparator)
|
||||
} else if (value.length == 6) {
|
||||
return [value.substring(0, 2), value.substring(2, 4), value.substring(4, 6)].join(dateSeparator)
|
||||
return [
|
||||
value.substring(0, 2),
|
||||
value.substring(2, 4),
|
||||
value.substring(4, 6),
|
||||
].join(dateSeparator)
|
||||
} else if (value.length == 8 && inputFormat.substring(0, 4) != 'yyyy') {
|
||||
return [value.substring(0, 2), value.substring(2, 4), value.substring(4, 8)].join(dateSeparator)
|
||||
return [
|
||||
value.substring(0, 2),
|
||||
value.substring(2, 4),
|
||||
value.substring(4, 8),
|
||||
].join(dateSeparator)
|
||||
} else if (value.length == 8 && inputFormat.substring(0, 4) == 'yyyy') {
|
||||
return [value.substring(0, 4), value.substring(4, 6), value.substring(6, 8)].join(dateSeparator)
|
||||
return [
|
||||
value.substring(0, 4),
|
||||
value.substring(4, 6),
|
||||
value.substring(6, 8),
|
||||
].join(dateSeparator)
|
||||
} else {
|
||||
return value
|
||||
}
|
||||
@@ -71,9 +100,9 @@ export class LocalizedDateParserFormatter extends NgbDateParserFormatter {
|
||||
let dateStruct = {
|
||||
day: +match.groups.day,
|
||||
month: +match.groups.month,
|
||||
year: +match.groups.year
|
||||
year: +match.groups.year,
|
||||
}
|
||||
if (dateStruct.year <= (new Date().getFullYear() - 2000)) {
|
||||
if (dateStruct.year <= new Date().getFullYear() - 2000) {
|
||||
dateStruct.year += 2000
|
||||
} else if (dateStruct.year < 100) {
|
||||
dateStruct.year += 1900
|
||||
@@ -87,9 +116,9 @@ export class LocalizedDateParserFormatter extends NgbDateParserFormatter {
|
||||
format(date: NgbDateStruct | null): string {
|
||||
if (date) {
|
||||
return this.getDateInputFormat()
|
||||
.replace('dd', date.day.toString().padStart(2, '0'))
|
||||
.replace('mm', date.month.toString().padStart(2, '0'))
|
||||
.replace('yyyy', date.year.toString().padStart(4, '0'))
|
||||
.replace('dd', date.day.toString().padStart(2, '0'))
|
||||
.replace('mm', date.month.toString().padStart(2, '0'))
|
||||
.replace('yyyy', date.year.toString().padStart(4, '0'))
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
|
@@ -1,16 +1,15 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { NgbDateAdapter, NgbDateStruct } from "@ng-bootstrap/ng-bootstrap";
|
||||
import { Injectable } from '@angular/core'
|
||||
import { NgbDateAdapter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'
|
||||
|
||||
@Injectable()
|
||||
export class ISODateAdapter extends NgbDateAdapter<string> {
|
||||
|
||||
fromModel(value: string | null): NgbDateStruct | null {
|
||||
if (value) {
|
||||
let date = new Date(value)
|
||||
return {
|
||||
day : date.getDate(),
|
||||
month : date.getMonth() + 1,
|
||||
year : date.getFullYear()
|
||||
day: date.getDate(),
|
||||
month: date.getMonth() + 1,
|
||||
year: date.getFullYear(),
|
||||
}
|
||||
} else {
|
||||
return null
|
||||
@@ -19,7 +18,13 @@ export class ISODateAdapter extends NgbDateAdapter<string> {
|
||||
|
||||
toModel(date: NgbDateStruct | null): string | null {
|
||||
if (date) {
|
||||
return date.year.toString().padStart(4, '0') + "-" + date.month.toString().padStart(2, '0') + "-" + date.day.toString().padStart(2, '0')
|
||||
return (
|
||||
date.year.toString().padStart(4, '0') +
|
||||
'-' +
|
||||
date.month.toString().padStart(2, '0') +
|
||||
'-' +
|
||||
date.day.toString().padStart(2, '0')
|
||||
)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
|
@@ -1,16 +1,15 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { NgbDateAdapter, NgbDateStruct } from "@ng-bootstrap/ng-bootstrap";
|
||||
import { Injectable } from '@angular/core'
|
||||
import { NgbDateAdapter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'
|
||||
|
||||
@Injectable()
|
||||
export class ISODateTimeAdapter extends NgbDateAdapter<string> {
|
||||
|
||||
fromModel(value: string | null): NgbDateStruct | null {
|
||||
if (value) {
|
||||
let date = new Date(value)
|
||||
return {
|
||||
day : date.getDate(),
|
||||
month : date.getMonth() + 1,
|
||||
year : date.getFullYear()
|
||||
day: date.getDate(),
|
||||
month: date.getMonth() + 1,
|
||||
year: date.getFullYear(),
|
||||
}
|
||||
} else {
|
||||
return null
|
||||
@@ -18,6 +17,8 @@ export class ISODateTimeAdapter extends NgbDateAdapter<string> {
|
||||
}
|
||||
|
||||
toModel(date: NgbDateStruct | null): string | null {
|
||||
return date ? new Date(date.year, date.month - 1, date.day).toISOString() : null
|
||||
return date
|
||||
? new Date(date.year, date.month - 1, date.day).toISOString()
|
||||
: null
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user