TypeScript封装一个根据背景色显示适合的字体颜色

开发 前端
在TypeScript中,你可以创建一个函数来确定基于背景颜色的对比色(通常是黑色或白色)作为文本颜色,以确保文本的可读性。

方法一

在TypeScript中,你可以创建一个函数来确定基于背景颜色的对比色(通常是黑色或白色)作为文本颜色,以确保文本的可读性。为了计算背景颜色与黑白之间的对比度,我们可以使用Web内容可访问性指南(WCAG)的公式。

下面是一个简单的TypeScript函数示例,它接受一个背景颜色(十六进制格式)作为输入,然后返回一个推荐的字体颜色(黑色或白色):

function getContrastYIQ(hexcolor: string): string {
  // 如果输入不带#,则添加
  if (hexcolor.indexOf('#') !== 0) {
    hexcolor = '#' + hexcolor;
  }

  // 去除#号
  hexcolor = hexcolor.replace('#', '');

  // 将十六进制颜色分解为r, g, b
  const r = parseInt(hexcolor.substr(0, 2), 16);
  const g = parseInt(hexcolor.substr(2, 2), 16);
  const b = parseInt(hexcolor.substr(4, 2), 16);

  // 使用YIQ方程式计算亮度
  const yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;

  // 根据亮度返回黑色或白色
  return yiq >= 128 ? 'black' : 'white';
}

// 用法示例
const backgroundColor = '#005A31'; // 深绿色
const textColor = getContrastYIQ(backgroundColor);
console.log(textColor); // 应该返回'white'因为深绿色背景下白色文本更可读

这个函数使用了YIQ色彩空间,这是一个电视广播领域使用的色彩空间,其中Y代表亮度分量,而I和Q分别代表色度分量。在这个例子中,我们只关心Y分量(亮度),以决定文本颜色使用黑色还是白色。

请确保输入的颜色格式正确,并且你的应用可以处理不同的输入格式。上面的代码假设输入是一个六位的十六进制颜色值。

此外,还有更复杂的方法来判断对比度,比如根据WCAG 2.0标准计算颜色之间的对比度比率。这样的计算会更复杂一些,但也可以提供更精确的结果。

方法二

如果你希望函数能够返回除黑白之外的其他颜色作为字体颜色,你可以通过定义一组颜色和它们的亮度阈值来实现。这样,函数就可以根据背景颜色的亮度来选择一个合适的字体颜色。

首先,我们定义一个简单的亮度判断函数,然后基于背景色的亮度选择合适的字体颜色。这里举例说明如何扩展前面的函数以支持多种颜色选择。

function getBrightness(hexcolor: string): number {
  // 标准化十六进制格式
  if (!hexcolor.startsWith('#')) {
    hexcolor = '#' + hexcolor;
  }

  // 提取RGB组件
  const r = parseInt(hexcolor.substring(1, 3), 16);
  const g = parseInt(hexcolor.substring(3, 5), 16);
  const b = parseInt(hexcolor.substring(5, 7), 16);

  // 计算亮度
  return ((r * 299) + (g * 587) + (b * 114)) / 1000;
}

function chooseTextColor(backgroundHex: string): string {
  // 定义颜色选项和它们的亮度界限
  const colors = [
    { color: '#000000', minBrightness: 0, maxBrightness: 180 },   // 黑色
    { color: '#FFFFFF', minBrightness: 180, maxBrightness: 256 }, // 白色
    { color: '#FFD700', minBrightness: 100, maxBrightness: 220 }, // 金色
    { color: '#0000FF', minBrightness: 50, maxBrightness: 150 }   // 蓝色
  ];

  // 获取背景色亮度
  const backgroundBrightness = getBrightness(backgroundHex);

  // 选择一个亮度合适的颜色
  const suitableColor = colors.find(c => backgroundBrightness >= c.minBrightness && backgroundBrightness < c.maxBrightness);
  return suitableColor ? suitableColor.color : '#FFFFFF'; // 默认返回白色
}

// 用法示例
const backgroundColor = '#005A31'; // 深绿色
const textColor = chooseTextColor(backgroundColor);
console.log(textColor); // 根据配置,可能返回不同颜色

在这个例子中,我为函数增加了几种颜色选择。函数chooseTextColor将检查背景颜色的亮度,并找到一个在设定亮度范围内的颜色作为文本颜色。你可以根据需要调整颜色选项和它们的亮度界限。

责任编辑:姜华 来源: 今日头条
相关推荐

2022-12-26 11:25:25

CSS黑白文字

2024-01-30 13:53:31

2021-04-16 05:54:05

CSS 文字动画技巧

2010-08-11 16:41:30

Flex DataGr

2010-08-11 16:30:49

Flex DataGr

2023-11-09 09:02:26

TypeScriptas const

2015-06-08 11:21:42

iOS技巧

2024-04-02 09:42:39

2013-08-27 14:03:33

Web设计设计字体

2024-01-26 12:35:25

JavaScript项目软件包

2024-04-09 07:36:03

AI产品AI技术人工智能

2024-04-07 00:00:01

TypeScript语言REST

2015-09-08 10:32:21

开源项目选择方式

2020-06-18 10:03:13

在家工作疫情统一通信

2015-09-11 10:29:13

开源项目阅读

2023-07-17 09:19:20

CSSCSS 渐变

2018-11-29 09:45:03

Windows 10Windows版本

2020-06-17 15:00:27

FedoraUbuntuLinux

2023-10-08 11:53:29

2013-12-19 14:13:16

Android ApiAndroid开发Android SDK
点赞
收藏

51CTO技术栈公众号