html字体颜色反色,根据背景颜色反转CSS字体颜色

f0809963bc216ef16ea4d64b1d7aac07.png

慕侠2389804

我知道这是一个老问题,但我想在我得知之前添加另一个我一直在使用的解决方案mix-blend-mode。我们的想法是将信息复制在两个层中,即背景和前景,其中背景和前景具有不同的背景和文本颜色。这些在尺寸和文字方面是相同的。在这两者之间,我使用剪切框div将顶层裁剪为所需的宽度,显示未剪裁的顶层,并显示裁剪窗口外的背景图层。这与接受的答案中的“Two div”解决方案类似,但使用额外的剪辑框。如果需要,它允许轻松居中文本,并简单,直接地选择颜色。HTML:

  display: block;

  margin: 0;

  /* Choose desired padding/height in coordination with font size */

  padding: 10px;

  height: 28px;}#background {

  position: relative;

  /* Choose a border to your liking, or none */

  border: 1px solid lightgray;

  /* Choose your desired text attributes */

  text-align: center;

  font-family: Calibri, "Sans Serif";

  font-size: 16pt;

  /* Set the desired width of the whole progress bar */

  width: 75%;

  /* Choose the desired background and text color */

  background-color: white;

  color: black;}#foreground {

  position: absolute;

  left: 0;

  top: 0;

  /* Choose the desired background and text colors */

  background-color: navy;

  color: white;}#boundbox {

  position: absolute;

  left: 0;

  top: 0;

  overflow: hidden;}我使用jQuery以编程方式设置百分比进度,并确保前景的宽度与背景的宽度匹配,并且它们具有相同的文本。这也可以使用纯Javascript轻松完成。// Set foreground width to background width// Do this after DOM is ready$('#foreground').width($('#background').width())// Based upon an event that determines a content change// you can set the text as in the below examplepercent_complete = 45   /* Compute a % complete value */$('#foreground').text(`${percent_complete}% complete`)$('#background span').text($('#foreground').text())$('#boundbox').css('width', `${percent_complete}%`)这是一个小提琴:进度条。我在Chrome,Firefox和Microsoft Edge中对此进行了测试。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在屏幕上绘制反色字符,即将字符的颜色设置为屏幕上对应位置的背景颜色反色,可以采用以下步骤: 1. 获取屏幕上指定位置的颜色值。 2. 计算背景颜色反色。 3. 将文本颜色设置为背景颜色反色。 4. 在屏幕上绘制字符。 下面是一个简单的示例代码: ```cpp #include <iostream> #include <windows.h> int main() { HWND desktopWindow = GetDesktopWindow(); HDC hdc = GetDC(desktopWindow); int x = 100; // 字符绘制的横坐标 int y = 100; // 字符绘制的纵坐标 wchar_t text[] = L"Hello, C++!"; // 要显示的文本 COLORREF bgColor = GetPixel(hdc, x, y); // 获取屏幕上指定位置的颜色值 // 计算背景颜色反色 COLORREF inverseColor = RGB(255 - GetRValue(bgColor), 255 - GetGValue(bgColor), 255 - GetBValue(bgColor)); // 设置文本颜色为背景颜色反色 SetTextColor(hdc, inverseColor); // 在屏幕上绘制字符 TextOutW(hdc, x, y, text, wcslen(text)); ReleaseDC(desktopWindow, hdc); return 0; } ``` 在上述代码中,我们通过`GetDesktopWindow`函数获取桌面窗口的句柄,然后使用`GetDC`函数获取窗口的设备上下文。接下来,使用`GetPixel`函数获取指定位置的颜色值。然后,通过计算背景颜色反色,使用`RGB`宏来设置文本颜色为反色。最后,使用`TextOutW`函数在屏幕上绘制字符。 请注意,此代码仅适用于Windows平台,并依赖于Windows API。在特定的屏幕区域上获取颜色值可能需要管理员权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值