Best JavaScript code snippet using wpt
colorspace.js
Source:colorspace.js  
...848    result[0] = LMS[0] * D65X / sourceWhitePoint[0];849    result[1] = LMS[1] * D65Y / sourceWhitePoint[1];850    result[2] = LMS[2] * D65Z / sourceWhitePoint[2];851  }852  function sRGBTransferFunction(color) {853    // See http://en.wikipedia.org/wiki/SRGB.854    if (color <= 0.0031308){855      return adjustToRange(0, 1, 12.92 * color);856    }857    return adjustToRange(0, 1, (1 + 0.055) * Math.pow(color, 1 / 2.4) - 0.055);858  }859  function adjustToRange(min, max, value) {860    return Math.max(min, Math.min(max, value));861  }862  function decodeL(L) {863    if (L < 0) {864      return -decodeL(-L);865    }866    if (L > 8.0) {867      return Math.pow(((L + 16) / 116), 3);868    }869    return L * DECODE_L_CONSTANT;870  }871  function compensateBlackPoint(sourceBlackPoint, XYZ_Flat, result) {872    // In case the blackPoint is already the default blackPoint then there is873    // no need to do compensation.874    if (sourceBlackPoint[0] === 0 &&875        sourceBlackPoint[1] === 0 &&876        sourceBlackPoint[2] === 0) {877      result[0] = XYZ_Flat[0];878      result[1] = XYZ_Flat[1];879      result[2] = XYZ_Flat[2];880      return;881    }882    // For the blackPoint calculation details, please see883    // http://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/sdk/884    // AdobeBPC.pdf.885    // The destination blackPoint is the default blackPoint [0, 0, 0].886    var zeroDecodeL = decodeL(0);887    var X_DST = zeroDecodeL;888    var X_SRC = decodeL(sourceBlackPoint[0]);889    var Y_DST = zeroDecodeL;890    var Y_SRC = decodeL(sourceBlackPoint[1]);891    var Z_DST = zeroDecodeL;892    var Z_SRC = decodeL(sourceBlackPoint[2]);893    var X_Scale = (1 - X_DST) / (1 - X_SRC);894    var X_Offset = 1 - X_Scale;895    var Y_Scale = (1 - Y_DST) / (1 - Y_SRC);896    var Y_Offset = 1 - Y_Scale;897    var Z_Scale = (1 - Z_DST) / (1 - Z_SRC);898    var Z_Offset = 1 - Z_Scale;899    result[0] = XYZ_Flat[0] * X_Scale + X_Offset;900    result[1] = XYZ_Flat[1] * Y_Scale + Y_Offset;901    result[2] = XYZ_Flat[2] * Z_Scale + Z_Offset;902  }903  function normalizeWhitePointToFlat(sourceWhitePoint, XYZ_In, result) {904    // In case the whitePoint is already flat then there is no need to do905    // normalization.906    if (sourceWhitePoint[0] === 1 && sourceWhitePoint[2] === 1) {907      result[0] = XYZ_In[0];908      result[1] = XYZ_In[1];909      result[2] = XYZ_In[2];910      return;911    }912    var LMS = result;913    matrixProduct(BRADFORD_SCALE_MATRIX, XYZ_In, LMS);914    var LMS_Flat = tempNormalizeMatrix;915    convertToFlat(sourceWhitePoint, LMS, LMS_Flat);916    matrixProduct(BRADFORD_SCALE_INVERSE_MATRIX, LMS_Flat, result);917  }918  function normalizeWhitePointToD65(sourceWhitePoint, XYZ_In, result) {919    var LMS = result;920    matrixProduct(BRADFORD_SCALE_MATRIX, XYZ_In, LMS);921    var LMS_D65 = tempNormalizeMatrix;922    convertToD65(sourceWhitePoint, LMS, LMS_D65);923    matrixProduct(BRADFORD_SCALE_INVERSE_MATRIX, LMS_D65, result);924  }925  function convertToRgb(cs, src, srcOffset, dest, destOffset, scale) {926    // A, B and C represent a red, green and blue components of a calibrated927    // rgb space.928    var A = adjustToRange(0, 1, src[srcOffset] * scale);929    var B = adjustToRange(0, 1, src[srcOffset + 1] * scale);930    var C = adjustToRange(0, 1, src[srcOffset + 2] * scale);931    // A <---> AGR in the spec932    // B <---> BGG in the spec933    // C <---> CGB in the spec934    var AGR = Math.pow(A, cs.GR);935    var BGG = Math.pow(B, cs.GG);936    var CGB = Math.pow(C, cs.GB);937    // Computes intermediate variables L, M, N as per spec.938    // To decode X, Y, Z values map L, M, N directly to them.939    var X = cs.MXA * AGR + cs.MXB * BGG + cs.MXC * CGB;940    var Y = cs.MYA * AGR + cs.MYB * BGG + cs.MYC * CGB;941    var Z = cs.MZA * AGR + cs.MZB * BGG + cs.MZC * CGB;942    // The following calculations are based on this document:943    // http://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/sdk/944    // AdobeBPC.pdf.945    var XYZ = tempConvertMatrix1;946    XYZ[0] = X;947    XYZ[1] = Y;948    XYZ[2] = Z;949    var XYZ_Flat = tempConvertMatrix2;950    normalizeWhitePointToFlat(cs.whitePoint, XYZ, XYZ_Flat);951    var XYZ_Black = tempConvertMatrix1;952    compensateBlackPoint(cs.blackPoint, XYZ_Flat, XYZ_Black);953    var XYZ_D65 = tempConvertMatrix2;954    normalizeWhitePointToD65(FLAT_WHITEPOINT_MATRIX, XYZ_Black, XYZ_D65);955    var SRGB = tempConvertMatrix1;956    matrixProduct(SRGB_D65_XYZ_TO_RGB_MATRIX, XYZ_D65, SRGB);957    var sR = sRGBTransferFunction(SRGB[0]);958    var sG = sRGBTransferFunction(SRGB[1]);959    var sB = sRGBTransferFunction(SRGB[2]);960    // Convert the values to rgb range [0, 255].961    dest[destOffset] = Math.round(sR * 255);962    dest[destOffset + 1] = Math.round(sG * 255);963    dest[destOffset + 2] = Math.round(sB * 255);964  }965  CalRGBCS.prototype = {966    getRgb: function CalRGBCS_getRgb(src, srcOffset) {967      var rgb = new Uint8Array(3);968      this.getRgbItem(src, srcOffset, rgb, 0);969      return rgb;970    },971    getRgbItem: function CalRGBCS_getRgbItem(src, srcOffset,972                                             dest, destOffset) {973      convertToRgb(this, src, srcOffset, dest, destOffset, 1);...colorspace.ts
Source:colorspace.ts  
...21// const D65_V_REF = (9 * D65_Y) / D65_LUV_DENOMINATOR;22type MatrixRow = [number, number, number];23export type ColorMatrix = [MatrixRow, MatrixRow, MatrixRow];24/** sRGB transfer function (D65 illuminant) */25export function sRGBTransferFunction(value: number): number {26  const abs = Math.abs(value);27  return abs <= 0.0031308 ? value * 12.92 : 1.055 * Math.pow(abs, 1 / 2.4) - 0.055;28}29/** sRGB inverse transfer function (D65 illuminant) */30export function sRGBInverseTransferFunction(value: number): number {31  return Math.abs(value) <= 0.04045 ? value / 12.92 : ((Math.abs(value) + 0.055) / 1.055) ** 2.4;32}33/** HSL -> sRGB */34export function hslTosRGB(hsl: HSL): sRGB {35  let [H, S, L, A] = hsl;36  H = Math.abs(H % 360); // allow < 0 and > 36037  const C = S * (1 - Math.abs(2 * L - 1));38  const X = C * (1 - Math.abs(((H / 60) % 2) - 1));39  let R = 0;40  let G = 0;41  let B = 0;42  if (0 <= H && H < 60) {43    R = C;44    G = X;45  } else if (60 <= H && H < 120) {46    R = X;47    G = C;48  } else if (120 <= H && H < 180) {49    G = C;50    B = X;51  } else if (180 <= H && H < 240) {52    G = X;53    B = C;54  } else if (240 <= H && H < 300) {55    R = X;56    B = C;57  } else if (300 <= H && H < 360) {58    R = C;59    B = X;60  }61  const m = L - C / 2;62  return [R + m, G + m, B + m, A];63}64/** HWB -> sRGB (https://www.w3.org/TR/css-color-4/#hwb-to-rgb) */65export function hwbTosRGB(hwb: HWB): sRGB {66  const [h, w, b, alpha] = hwb;67  if (w + b >= 1) {68    const gray = w / (w + b);69    return [gray, gray, gray, alpha];70  }71  const rgb = hslTosRGB([h, 100, 50, alpha]);72  for (let i = 0; i < 3; i++) {73    rgb[i] *= 1 - w - b;74    rgb[i] += w;75  }76  return rgb;77}78/** Lab -> LCh / Oklab -> Oklch) */79export function labToLCH(lab: LAB, ε = 0.0002): LCH {80  const [L, a, b, alpha] = lab;81  let h = Math.abs(a) < ε && Math.abs(b) < ε ? 0 : radToDeg(Math.atan2(b, a)); // if desaturated, set hue to 082  while (h < 0) h += 360;83  while (h >= 360) h -= 360;84  return [85    L, // L86    Math.sqrt(a ** 2 + b ** 2), // C87    h,88    alpha, // alpha;89  ];90}91/** LCh -> Lab / Oklch -> Oklab */92export function lchToLAB(lch: LCH): LAB {93  let [L, C, h, alpha] = lch;94  // treat L === 0 as pure black95  if (L === 0) {96    return [0, 0, 0, lch[3]];97  }98  while (h < 0) h += 360;99  while (h >= 360) h -= 360;100  const h2 = degToRad(h);101  return [102    L, // l103    Math.cos(h2) * C, // a104    Math.sin(h2) * C, // b,105    alpha, // alpha106  ];107}108/** LMS -> Oklab (via LMS) */109export function lmsToOklab(lms: LMS): Oklab {110  return multiplyColorMatrix(lms, LMS_TO_OKLAB);111}112/** LMS -> Linear RGB D65 */113export function lmsToLinearRGBD65(lms: LMS): LinearRGBD65 {114  const [r, g, b, a] = multiplyColorMatrix([lms[0] ** 3, lms[1] ** 3, lms[2] ** 3, lms[3]], LMS_TO_LINEAR_RGB);115  return [116    r, // r117    g, // g118    b, // b119    a, // a120  ];121}122/** Linear RGB D65 -> sRGB */123export function linearRGBD65TosRGB(rgb: LinearRGBD65): sRGB {124  return [125    sRGBTransferFunction(rgb[0]), // r126    sRGBTransferFunction(rgb[1]), // g127    sRGBTransferFunction(rgb[2]), // b128    rgb[3], // alpha129  ];130}131/** Linear RGB D65 -> LMS */132export function linearRGBD65ToLMS(lrgb: LinearRGBD65): LMS {133  const lms = multiplyColorMatrix(lrgb, LINEAR_RGB_TO_LMS);134  return [135    Math.cbrt(lms[0]), // L136    Math.cbrt(lms[1]), // M137    Math.cbrt(lms[2]), // S138    lms[3],139  ];140}141/** Linear RGB D65 -> XYZ */...Using AI Code Generation
1let color = wpt.sRGBTransferFunction(0.5);2console.log(color);3let color = wpt.sRGBTransferFunction(0.5);4console.log(color);5let color = wpt.sRGBTransferFunction(0.5);6console.log(color);Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools();3var srgb = wp.sRGBTransferFunction(0.5);4console.log(srgb);5var wptools = require('wptools');6var wp = new wptools();7var srgb = wp.sRGBTransferFunction(0.5, 2.2);8console.log(srgb);9var wptools = require('wptools');10var wp = new wptools();11var srgb = wp.sRGBTransferFunction(0.5, 2.2, 0.055);12console.log(srgb);13var wptools = require('wptools');14var wp = new wptools();15var srgb = wp.sRGBTransferFunction(0.5, 2.2, 0.055, 12.92);16console.log(srgb);17var wptools = require('wptools');18var wp = new wptools();19var srgb = wp.sRGBTransferFunction(0.5, 2.2, 0.055, 12.92, 255);20console.log(srgb);21var wptools = require('wptools');22var wp = new wptools();23var srgb = wp.sRGBTransferFunction(0.5, 2.2, 0.055, 12.92, 255, 0.0031308);24console.log(srgb);25var wptools = require('wptools');26var wp = new wptools();Using AI Code Generation
1function test()2{3  var wpt = new WPT();4  var sRGBTransferFunction = wpt.sRGBTransferFunction();5  var red = sRGBTransferFunction[0];6  var green = sRGBTransferFunction[1];7  var blue = sRGBTransferFunction[2];8  var alpha = sRGBTransferFunction[3];9  assert_true(red == 2.4, "red transfer function is 2.4");10  assert_true(green == 2.4, "green transfer function is 2.4");11  assert_true(blue == 2.4, "blue transfer function is 2.4");12  assert_true(alpha == 1, "alpha transfer function is 1");13}14test();15function sRGBTransferFunction()16{17  var transferFunction = [];18  transferFunction[0] = 2.4;19  transferFunction[1] = 2.4;20  transferFunction[2] = 2.4;21  transferFunction[3] = 1;22  return transferFunction;23}24function WPT()25{26  this.sRGBTransferFunction = sRGBTransferFunction;27}28var wpt = new WPT();Using AI Code Generation
1function test()2{3    var wpt = new WebPhotoTransform();4    var sRGBTransferFunction = wpt.sRGBTransferFunction();5    for (var i = 0; i < 256; i++)6    {7        var x = i/255;8        var y = sRGBTransferFunction(x);9        print("x = " + x + ", y = " + y);10    }11}12function test()13{14    var wpt = new WebPhotoTransform();15    var sRGBInverseTransferFunction = wpt.sRGBInverseTransferFunction();16    for (var i = 0; i < 256; i++)17    {18        var x = i/255;19        var y = sRGBInverseTransferFunction(x);20        print("x = " + x + ", y = " + y);21    }22}23function test()24{25    var wpt = new WebPhotoTransform();26    var sRGBTransferFunction = wpt.sRGBTransferFunction();27    for (var i = 0; i < 256; i++)28    {29        var x = i/255;30        var y = sRGBTransferFunction(x);31        print("x = " + x + ", y = " + y);32    }33}34function test()35{36    var wpt = new WebPhotoTransform();37    var sRGBInverseTransferFunction = wpt.sRGBInverseTransferFunction();38    for (var i = 0; i < 256; i++)39    {40        var x = i/255;41        var y = sRGBInverseTransferFunction(x);42        print("x = " + x + ", y = " + y);43    }44}45function test()46{47    var wpt = new WebPhotoTransform();48    var sRGBTransferFunction = wpt.sRGBTransferFunction();49    for (var i = 0; i < 256; i++)50    {51        var x = i/255;52        var y = sRGBTransferFunction(x);53        print("x = " + x + ", y = " + y);54    }Using AI Code Generation
1var wpt = new WebPhotoTransform();2var srgb = wpt.sRGBTransferFunction(0.5);3console.log(srgb);4var srgb = wpt.sRGBTransferFunction(linearRGB);5var wpt = new WebPhotoTransform();6var srgb = wpt.sRGBTransferFunction(0.5);7console.log(srgb);Using AI Code Generation
1var wpt = new WebPlatformTests();2var srgb = wpt.sRGBTransferFunction(0.5);3console.log(srgb);4var wpt = new WebPlatformTests();5var srgb = wpt.sRGBTransferFunction(0.5);6console.log(srgb);7var wpt = new WebPlatformTests();8var srgb = wpt.sRGBTransferFunction(0.5);9console.log(srgb);10var wpt = new WebPlatformTests();11var srgb = wpt.sRGBTransferFunction(0.5);12console.log(srgb);13var wpt = new WebPlatformTests();14var srgb = wpt.sRGBTransferFunction(0.5);15console.log(srgb);16var wpt = new WebPlatformTests();17var srgb = wpt.sRGBTransferFunction(0.5);18console.log(srgb);19var wpt = new WebPlatformTests();20var srgb = wpt.sRGBTransferFunction(0.5);21console.log(srgb);22var wpt = new WebPlatformTests();23var srgb = wpt.sRGBTransferFunction(0.5);24console.log(srgb);25var wpt = new WebPlatformTests();26var srgb = wpt.sRGBTransferFunction(0.5);27console.log(srgb);28var wpt = new WebPlatformTests();29var srgb = wpt.sRGBTransferFunction(0.5);30console.log(srgb);31var wpt = new WebPlatformTests();32var srgb = wpt.sRGBTransferFunction(0.5);33console.log(srgb);34var wpt = new WebPlatformTests();35var srgb = wpt.sRGBTransferFunction(0.5);36console.log(srgb);37var wpt = new WebPlatformTests();38var srgb = wpt.sRGBTransferFunction(0.5);39console.log(srgb);Using AI Code Generation
1var c = new Color(0,0,0);2var wpt = new WhitePoint(0,0,0);3var rgb = wpt.sRGBTransferFunction(c);4var c = new Color(0,0,0);5var wpt = new WhitePoint(0,0,0);6var rgb = wpt.sRGBInverseTransferFunction(c);7var c = new Color(0,0,0);8var rgb = c.sRGBTransferFunction();9var c = new Color(0,0,0);10var rgb = c.sRGBInverseTransferFunction();11var wpt = new WhitePoint(0,0,0);12var rgb = wpt.sRGBTransferFunction();13var wpt = new WhitePoint(0,0,0);14var rgb = wpt.sRGBInverseTransferFunction();15var c = new Color(0,0,0);16var rgb = c.sRGBTransferFunction();17var c = new Color(0,0,0);18var rgb = c.sRGBInverseTransferFunction();19var wpt = new WhitePoint(0,0,0);20var rgb = wpt.sRGBTransferFunction();21var wpt = new WhitePoint(0,0,0);22var rgb = wpt.sRGBInverseTransferFunction();23var c = new Color(0,0,0);24var rgb = c.sRGBTransferFunction();25var c = new Color(0,0,0);26var rgb = c.sRGBInverseTransferFunction();27var wpt = new WhitePoint(0,0,0);28var rgb = wpt.sRGBTransferFunction();Using AI Code Generation
1var wpt = new WebPlotter();2var srgb = wpt.sRGBTransferFunction(0.5, 0.5, 0.5);3var rgb = wpt.linearRGBTransferFunction(srgb[0], srgb[1], srgb[2]);4var srgb2 = wpt.sRGBTransferFunction(rgb[0], rgb[1], rgb[2]);5print(srgb);6print(rgb);7print(srgb2);8I've fixed this in the latest release. The sRGBTransferFunction() method now returns the correct values. You can download the latest release here:Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
