{"record":{"id":"5bfd392657feaf57","repo":"Yalantis/uCrop","slug":"hsltorgb-instance-is-not-a-hsl-image","errorCode":null,"errorMessage":"HSLtoRGB(): Instance is not a HSL image.","messagePattern":"HSLtoRGB\\(\\): Instance is not a HSL image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37175,"sourceCode":"          H = 60*(C==0?0:M==R?cimg::mod((G - B)/C,(Tfloat)6):M==G?(B - R)/C + 2:(R - G)/C + 4),\n          L = 0.5f*(m + M)/255,\n          S = L==1 || L==0?0:C/(1 - cimg::abs(2*L - 1))/255;\n        p1[N] = (T)H;\n        p2[N] = (T)S;\n        p3[N] = (T)L;\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from RGB to HSL color spaces \\newinstance.\n    CImg<Tfloat> get_RGBtoHSL() const {\n      return CImg<Tfloat>(*this,false).RGBtoHSL();\n    }\n\n    //! Convert pixel values from HSL to RGB color spaces.\n    CImg<T>& HSLtoRGB() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"HSLtoRGB(): Instance is not a HSL image.\",\n                                    cimg_instance);\n\n      T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);\n      const longT whd = (longT)width()*height()*depth();\n      cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))\n      for (longT N = 0; N<whd; ++N) {\n        const Tfloat\n          H = cimg::mod((Tfloat)p1[N]/60,(Tfloat)6),\n          S = (Tfloat)p2[N],\n          L = (Tfloat)p3[N],\n          C = (1 - cimg::abs(2*L - 1))*S,\n          X = C*(1 - cimg::abs(cimg::mod(H,(Tfloat)2) - 1)),\n          m = L - C/2;\n        Tfloat R, G, B;\n        switch ((int)H) {\n        case 0 : R = C; G = X; B = 0; break;\n        case 1 : R = X; G = C; B = 0; break;","sourceCodeStart":37157,"sourceCodeEnd":37193,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37157-L37193","documentation":"CImg<T>::HSLtoRGB() converts HSL values back to RGB and requires exactly 3 channels (H, S, L planes). Any other spectrum throws CImgInstanceException. It validates only the channel count, not that the stored values actually represent HSL.","triggerScenarios":"Calling img.HSLtoRGB() on grayscale (spectrum=1) or RGBA (spectrum=4) images, or after channel-count changes between RGBtoHSL() and HSLtoRGB().","commonSituations":"Round-trip interrupted by a grayscale save/load; alpha handling code that left 4 channels; applying the inverse conversion to single-channel luminance data.","solutions":["Ensure spectrum()==3 (replicate or drop channels as needed) before calling.","Strip alpha via channels(0,2) if present.","Verify round-trip integrity: assert(img.spectrum()==3) after RGBtoHSL and before HSLtoRGB.","Avoid intermediate format conversions that alter channel count mid-pipeline."],"exampleFix":"// before\nCImg<float> hsl(\"hsl.png\"); // saved as 4-channel RGBA\nhsl.HSLtoRGB(); // throws\n// after\nCImg<float> hsl(\"hsl.png\");\nif (hsl.spectrum() == 4) hsl.channels(0,2);\nhsl.HSLtoRGB();","handlingStrategy":"type-guard","validationCode":"template<typename T> bool is3Channel(const CImg<T>& img) { return img.spectrum() == 3; }\nif (is3Channel(img)) img.HSLtoRGB();","typeGuard":"template<typename T> bool isHslShape(const CImg<T>& img) { return img.spectrum() == 3; }","tryCatchPattern":"try {\n  img.HSLtoRGB();\n} catch (const CImgInstanceException& e) {\n  std::fprintf(stderr, \"HSLtoRGB requires 3 channels, image has %d\\n\", img.spectrum());\n}","preventionTips":["Assert spectrum()==3 between RGBtoHSL and HSLtoRGB.","Avoid intermediate grayscale saves in HSL round trips.","Strip alpha before conversion.","Check spectrum after every loading or resizing operation."],"tags":["cimg","colorspace","image-processing","channel-count"],"backgroundTag":"incompatible-source-type","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}