// Test surface colorspace conversion
var surface = use('surface');
var json = use('json');

// Create a test surface
var surf = surface({
  width: 640,
  height: 480,
  format: "rgb888"
});

log.console("Created surface:");
log.console("  Size:", surf.width + "x" + surf.height);
log.console("  Format:", surf.format);

// Fill with a test color
surf.fill([1, 0.5, 0.25, 1]); // Orange color

// Test 1: Convert format only (no colorspace change)
log.console("\nTest 1: Convert to RGBA8888 format only");
var converted1 = surf.convert("rgba8888");
log.console("  New format:", converted1.format);

// Test 2: Convert format and colorspace
log.console("\nTest 2: Convert to YUY2 format with JPEG colorspace");
var converted2 = surf.convert("yuy2", "jpeg");
log.console("  New format:", converted2.format);

// Test 3: Try different colorspaces
var colorspaces = ["srgb", "srgb_linear", "jpeg", "bt601_limited", "bt709_limited"];
var test_format = "rgba8888";

log.console("\nTest 3: Converting to", test_format, "with different colorspaces:");
for (var i = 0; i < colorspaces.length; i++) {
  try {
    var conv = surf.convert(test_format, colorspaces[i]);
    log.console("  " + colorspaces[i] + ": Success");
  } catch(e) {
    log.console("  " + colorspaces[i] + ": Failed -", e.message);
  }
}

// Test 4: YUV formats with appropriate colorspaces
log.console("\nTest 4: YUV format conversions:");
var yuv_tests = [
  {format: "yuy2", colorspace: "jpeg"},
  {format: "nv12", colorspace: "bt601_limited"},
  {format: "nv21", colorspace: "bt709_limited"},
  {format: "yvyu", colorspace: "bt601_full"}
];

for (var i = 0; i < yuv_tests.length; i++) {
  var test = yuv_tests[i];
  try {
    var conv = surf.convert(test.format, test.colorspace);
    log.console("  " + test.format + " with " + test.colorspace + ": Success");
  } catch(e) {
    log.console("  " + test.format + " with " + test.colorspace + ": Failed -", e.message);
  }
}

log.console("\nColorspace conversion test complete!");
$_.stop();