import { Diagnostic } from "@siteimprove/alfa-act" ; import { RGB } from "@siteimprove/alfa-css" ; import { Equatable } from "@siteimprove/alfa-equatable" ; import { Serializable } from "@siteimprove/alfa-json" ; import * as json from "@siteimprove/alfa-json" ; import { Hash , Hashable } from "@siteimprove/alfa-hash" ; export class Contrast extends Diagnostic { public static of ( message , threshold = 4.5 , pairings = [ ] ) { return new Contrast ( message , threshold , Array . from ( pairings ) ) ; } private readonly _threshold : number ; private readonly _pairings : Array < Contrast . Pairing > ; private constructor ( message , threshold , pairings ) { super ( message ) ; this . _threshold = threshold ; this . _pairings = pairings ; } public get threshold ( ) : number { return this . _threshold ; } public get pairings ( ) : Iterable < Contrast . Pairing > { return this . _pairings ; } public equals ( value ) { return ( super . equals ( value ) && value instanceof Contrast && value . _threshold === this . _threshold && value . _pairings . length === this . _pairings . length && value . _pairings . every ( ( pairing , i ) => pairing . equals ( this . _pairings [ i ] ) ) ) ; } public toJSON ( ) { return { ... super . toJSON ( ) , threshold : this . _threshold , pairings : this . _pairings . map ( ( pairing ) => pairing . toJSON ( ) ) , } ; } } export namespace Contrast { export interface JSON extends Diagnostic . JSON { threshold : number ; pairings : Array < Pairing . JSON > ; } export function isContrast ( value ) { return value instanceof Contrast ; } export class Pairing implements Equatable , Serializable , Hashable { public static of ( foreground , background , contrast ) { return new Pairing ( foreground , background , contrast ) ; } private readonly _foreground : RGB ; private readonly _background : RGB ; private readonly _contrast : number ; private constructor ( foreground , background , contrast ) { this . _foreground = foreground ; this . _background = background ; this . _contrast = contrast ; } public hash ( hash ) { hash . writeHashable ( this . _foreground ) . writeHashable ( this . _background ) . writeNumber ( this . _contrast ) ; } public get foreground ( ) : RGB { return this . _foreground ; } public get background ( ) : RGB { return this . _background ; } public get contrast ( ) : number { return this . _contrast ; } public equals ( value ) { return ( value instanceof Pairing && value . _foreground . equals ( this . _foreground ) && value . _background . equals ( this . _background ) && value . _contrast === this . _contrast ) ; } public toJSON ( ) { return { foreground : this . _foreground . toJSON ( ) , background : this . _background . toJSON ( ) , contrast : this . _contrast , } ; } } export namespace Pairing { export interface JSON { [ key ] : json . JSON ; foreground : RGB . JSON ; background : RGB . JSON ; contrast : number ; } } }