#ifndef CANVAS_H
#define CANVAS_H

#include "interval.h"
#include "vec3.h"

#include <cstdint>
#include <vector>

using Color = Vec3;

struct Rgb {
  uint8_t r, g, b;
};

inline Rgb to_rgb(const Color &c) {
  static const interval intensity(0.000, 0.999);
  return {static_cast<uint8_t>(256 * intensity.clamp(c.x())),
          static_cast<uint8_t>(256 * intensity.clamp(c.y())),
          static_cast<uint8_t>(256 * intensity.clamp(c.z()))};
}

struct Canvas {
  std::vector<Rgb> pixels;
  int width;
  int height;

  Canvas(int w, int h) : pixels(w * h), width(w), height(h) {}

  void set_pixel(int x, int y, Rgb color) {
    if (x >= 0 && x < width && y >= 0 && y < height) {
      pixels[y * width + x] = color;
    }
  }

  void set_pixel(int x, int y, Color color) {
    if (x >= 0 && x < width && y >= 0 && y < height) {
      pixels[y * width + x] = to_rgb(color);
    }
  }

  Rgb get_pixel(int x, int y) const { return pixels[y * width + x]; }
};

#endif
