UNPKG

theme-vir

Version:
164 lines (163 loc) 7.34 kB
import { assertWrap } from '@augment-vir/assert'; import { arrayToObject, round } from '@augment-vir/common'; // @ts-expect-error: `fontLookupAPCA` is not in the types import { calcAPCA, fontLookupAPCA } from 'apca-w3'; /** * Calculate contrast for the given color combination. * * @category Internal */ export function calculateContrast({ background, foreground, }) { const contrast = round(Number(calcAPCA(foreground, background)), { digits: 1 }); return { contrast, fontSizes: calculateFontSizes(contrast), contrastLevel: determineContrastLevel(contrast), }; } /** * Calculated needed font sizes for each font weight for the given color contrast. * * @category Internal */ export function calculateFontSizes(contrast) { const fontLookup = fontLookupAPCA(contrast).slice(1); const sizes = arrayToObject(fontLookup, (value, index) => { return { key: (index + 1) * 100, value, }; }); return sizes; } /** * Finds the color contrast level for the given contrast. * * @category Internal */ export function determineContrastLevel(contrast) { return assertWrap.isDefined(contrastLevels.find((threshold) => threshold.min <= Math.abs(contrast))); } /** * Names for each {@link ContrastLevel}. * * @category Internal */ export var ContrastLevelName; (function (ContrastLevelName) { ContrastLevelName["SmallBodyText"] = "small-body-text"; ContrastLevelName["BodyText"] = "body-text"; ContrastLevelName["NonBodyText"] = "non-body-text"; ContrastLevelName["LargeText"] = "large-text"; ContrastLevelName["SpotText"] = "spot-text"; ContrastLevelName["Decoration"] = "decoration"; ContrastLevelName["Invisible"] = "invisible"; })(ContrastLevelName || (ContrastLevelName = {})); /** * User-facing labels for {@link ContrastLevelName}. * * @category Internal */ export const contrastLevelLabel = { [ContrastLevelName.SmallBodyText]: 'Small Text', [ContrastLevelName.BodyText]: 'Body Text', [ContrastLevelName.NonBodyText]: 'Non-body Text', [ContrastLevelName.LargeText]: 'Headers', [ContrastLevelName.SpotText]: 'Placeholders', [ContrastLevelName.Decoration]: 'Decorations', [ContrastLevelName.Invisible]: 'Invisible ', }; /** * All {@link ContrastLevelName} values in order from highest contrast to lowest. * * @category Internal */ export const orderedContrastLevelNames = [ ContrastLevelName.SmallBodyText, ContrastLevelName.BodyText, ContrastLevelName.NonBodyText, ContrastLevelName.LargeText, ContrastLevelName.SpotText, ContrastLevelName.Decoration, ContrastLevelName.Invisible, ]; /** * All color contrast levels corresponding to APCA bronze guidelines. * * @category Internal */ export const contrastLevels = [ { min: 90, name: ContrastLevelName.SmallBodyText, description: 'Perfect for all sizes of text, even small body text.', apcaName: 'small body text only', apcaDescription: 'Preferred level for fluent text and columns of body text with a font no smaller than 18px/weight 300 or 14px/weight 400 (normal), or non-body text with a font no smaller than 12px. Also a recommended minimum for extremely thin fonts with a minimum of 24px at weight 200. Lc 90 is a suggested maximum for very large and bold fonts (greater than 36px bold), and large areas of color.', }, { min: 75, name: ContrastLevelName.BodyText, description: 'Good for regular body text and anything larger.', apcaName: 'body text okay', apcaDescription: 'The minimum level for columns of body text with a font no smaller than 24px/300 weight, 18px/400, 16px/500 and 14px/700. This level may be used with non-body text with a font no smaller than 15px/400. Also, Lc 75 should be considered a minimum for larger for any larger text where readability is important.', }, { min: 60, name: ContrastLevelName.NonBodyText, description: 'Good for legible non-body text and anything larger.', apcaName: 'fluent text only', apcaDescription: 'The minimum level recommended for content text that is not body, column, or block text. In other words, text you want people to read. The minimums: no smaller than 48px/200, 36px/300, 24px normal weight (400), 21px/500, 18px/600, 16px/700 (bold). These values based on the reference font Helvetica. To use these sizes as body text, add Lc 15 to the minimum contrast.', }, { min: 45, name: ContrastLevelName.LargeText, description: 'Okay for large or headline text.', apcaName: 'large & sub-fluent text', apcaDescription: 'The minimum for larger, heavier text (36px normal weight or 24px bold) such as headlines, and large text that should be fluently readable but is not body text. This is also the minimum for pictograms with fine details, or smaller outline icons, , no less than 4px in its smallest dimension.', }, { min: 30, name: ContrastLevelName.SpotText, description: 'Okay for disabled or placeholder text, copyright lines, icons, or non-text elements.', apcaName: 'spot & non text only', apcaDescription: 'The absolute minimum for any text not listed above, which means non-content text considered as "spot readable". This includes placeholder text and disabled element text, and some non-content like a copyright bug. This is also the minimum for large/solid semantic & understandable non-text elements such as "mostly solid" icons or pictograms, no less than 10px in its smallest dimension.', }, { min: 15, name: ContrastLevelName.Decoration, description: 'Only okay for decorations like graphics, borders, dividers, etc. Do not use for any text.', apcaName: 'no text usage', apcaDescription: 'The absolute minimum for any non-text that needs to be discernible and differentiable, but does not apply to semantic non-text such as icons, and is no less than 15px in its smallest dimension. This may include dividers, and in some cases large buttons or thick focus visible outlines, but does not include fine details which have a higher minimum. Designers should treat anything below this level as invisible, as it will not be visible for many users. This minimum level should be avoided for any items important to the use, understanding, or interaction of the site.', }, { min: 0, name: ContrastLevelName.Invisible, description: 'Effectively invisible for users.', apcaName: 'invisible', apcaDescription: 'This should be treated as invisible.', }, ]; /** * A mapping of all color contrast levels mins to their contrast levels. Generated from * {@link contrastLevels}. * * @category Internal */ export const contrastLevelMinMap = arrayToObject(contrastLevels, (contrastLevel) => { return { key: contrastLevel.min, value: contrastLevel, }; }); /** * A mapping of all color contrast levels mins to their contrast levels. Generated from * {@link contrastLevels}. * * @category Internal */ export const contrastLevelNameMap = arrayToObject(contrastLevels, (contrastLevel) => { return { key: contrastLevel.name, value: contrastLevel, }; });