private static XMVector HueToClr(XMVector p, XMVector q, XMVector h) { XMVector oneSixth = XMVector.FromFloat(1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f); XMVector twoThirds = XMVector.FromFloat(2.0f / 3.0f, 2.0f / 3.0f, 2.0f / 3.0f, 2.0f / 3.0f); XMVector t = h; if (XMVector3.Less(t, XMGlobalConstants.Zero)) { t = XMVector.Add(t, XMGlobalConstants.One); } if (XMVector3.Greater(t, XMGlobalConstants.One)) { t = XMVector.Subtract(t, XMGlobalConstants.One); } if (XMVector3.Less(t, oneSixth)) { // p + (q - p) * 6 * t XMVector t1 = XMVector.Subtract(q, p); XMVector t2 = XMVector.Multiply(XMGlobalConstants.Six, t); return(XMVector.MultiplyAdd(t1, t2, p)); } if (XMVector3.Less(t, XMGlobalConstants.OneHalf)) { return(q); } if (XMVector3.Less(t, twoThirds)) { // p + (q - p) * 6 * (2/3 - t) XMVector t1 = XMVector.Subtract(q, p); XMVector t2 = XMVector.Multiply(XMGlobalConstants.Six, XMVector.Subtract(twoThirds, t)); return(XMVector.MultiplyAdd(t1, t2, p)); } return(p); }
public static XMVector RgbToHsl(XMVector rgb) { XMVector r = XMVector.SplatX(rgb); XMVector g = XMVector.SplatY(rgb); XMVector b = XMVector.SplatZ(rgb); XMVector min = XMVector.Min(r, XMVector.Min(g, b)); XMVector max = XMVector.Max(r, XMVector.Max(g, b)); XMVector l = XMVector.Multiply(XMVector.Add(min, max), XMGlobalConstants.OneHalf); XMVector d = XMVector.Subtract(max, min); XMVector la = XMVector.Select(rgb, l, XMGlobalConstants.Select1110); if (XMVector3.Less(d, XMGlobalConstants.Epsilon)) { // Achromatic, assume H and S of 0 return(XMVector.Select(la, XMGlobalConstants.Zero, XMGlobalConstants.Select1100)); } else { XMVector s; XMVector h; XMVector d2 = XMVector.Add(min, max); if (XMVector3.Greater(l, XMGlobalConstants.OneHalf)) { // d / (2-max-min) s = XMVector.Divide(d, XMVector.Subtract(XMGlobalConstants.Two, d2)); } else { // d / (max+min) s = XMVector.Divide(d, d2); } if (XMVector3.Equal(r, max)) { // Red is max h = XMVector.Divide(XMVector.Subtract(g, b), d); } else if (XMVector3.Equal(g, max)) { // Green is max h = XMVector.Divide(XMVector.Subtract(b, r), d); h = XMVector.Add(h, XMGlobalConstants.Two); } else { // Blue is max h = XMVector.Divide(XMVector.Subtract(r, g), d); h = XMVector.Add(h, XMGlobalConstants.Four); } h = XMVector.Divide(h, XMGlobalConstants.Six); if (XMVector3.Less(h, XMGlobalConstants.Zero)) { h = XMVector.Add(h, XMGlobalConstants.One); } XMVector lha = XMVector.Select(la, h, XMGlobalConstants.Select1100); return(XMVector.Select(s, lha, XMGlobalConstants.Select1011)); } }