计算左值和宽度值的公式(基于 0% 和 100% 值)
如何找到基于百分比值并根据以下 0% 和 100% 值给出左值和宽度值的公式?
对于 0,00 百分比,左和宽度值将是:
- 宽度:658
- 左:11
对于 100,00 百分比,左和宽度值将是:
- 宽度:0(或 1)
- 左:670
我有一个渐变图像,其上我应用一种颜色来直观地表示百分比值。 渐变图像从 11 像素开始(左:11 表示 0,00%),宽度为 659(左:670 表示 100,00% = 659 + 11)
左和宽度值需要根据百分比,使颜色覆盖渐变。
如果百分比为 0%,则颜色将覆盖整个渐变(左:11,宽度:659)
------------------------------
|____________________________|
0 100
如果百分比为 100%,则颜色根本不会覆盖渐变(左:670,即 659 + 11,宽度: 1 或 0)
-
|____________________________|
0 100
How can I find the formula that would, based on a percentage value, give me left and width values based on the following 0% and 100% values?
For 0,00 percentage, left and with values would be:
- Width: 658
- Left: 11
For 100,00 percentage, left and width values would be:
- Width: 0 (or 1)
- Left: 670
I have a gradient image, on which I apply a color to give the percentage value a visual representation.
The gradient image starts at 11 pixels (Left: 11 for 0,00%) and has a width of 659 (Left: 670 for 100,00% = 659 + 11)
The left and width values needs to be calculate dynamically based on the percentage so that the color overlays the gradient.
If the percentage is 0%, the color would overlay the whole gradient (left: 11, width: 659)
------------------------------
|____________________________|
0 100
If the percentage is 100%, the color would not overlay the gradient at all (left: 670 which is 659 + 11, width: 1 or 0)
-
|____________________________|
0 100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要查找的公式可以表示为
f(x) = ax+b
。 f(x)是你想要得到的宽度,x是百分比,a & b 是(当前未知)常数。如果您有两个点(例如 0 和 100)的 f(x) 值,您可以导出如下公式:
现在您有了 a 和 b,您可以求解出您想要的任何百分比 x。上面的过程转换为伪代码如下:
现在您可以调用 getValueAtXPercent(50, 658, 1),它将返回 50% 的宽度值。
这是一个具体的实现,在Python中:
输出:
The formula you're looking for can be expressed as
f(x) = ax+b
. f(x) is the width you want to get, x is the percentage, and a & b are (currently unknown) constants.If you have the value of f(x) for two points (0 and 100 for instance), you can derive the formula like so:
Now that you have a and b, you can solve for any percentage x you want. The process above translates into pseudocode as:
now you can call getValueAtXPercent(50, 658, 1) and it will return your width value for 50%.
Here is a concrete implementation, in python:
output: