Пример #1
0
static bool write_foreground_color(unsigned char idx) {
    if (set_a_foreground && set_a_foreground[0]) {
        return write_color(set_a_foreground, idx, true);
    } else if (set_foreground && set_foreground[0]) {
        return write_color(set_foreground, idx, true);
    } else {
        return false;
    }
}
Пример #2
0
static bool write_background_color(unsigned char idx) {
    if (set_a_background && set_a_background[0]) {
        return write_color(set_a_background, idx, false);
    } else if (set_background && set_background[0]) {
        return write_color(set_background, idx, false);
    } else {
        return false;
    }
}
Пример #3
0
static void write_style_defs(FILE *f,const kate_style *ks,size_t indent)
{
  char *sindent=(char*)kate_malloc(1+indent);
  size_t n;
  for (n=0;n<indent;++n) sindent[n]=' ';
  sindent[indent]=0;

  {
    const char *halign=halign2text(ks->halign);
    const char *valign=valign2text(ks->valign);
    kate_color tc=ks->text_color,bc=ks->background_color,dc=ks->draw_color;
    fprintf(
      f,
      "%s%s %s\n",
      sindent,halign,valign
    );
    write_color(f,"text color",&tc,indent);
    write_color(f,"background color",&bc,indent);
    write_color(f,"draw color",&dc,indent);
    if (ks->font) {
      fprintf(f,"%sfont \"%s\"\n",sindent,ks->font);
    }
    if (ks->font_width>=0 || ks->font_height>=0) {
      if (ks->font_width==ks->font_height) {
        fprintf(f,"%sfont size %f%s\n",sindent,ks->font_height,metric2suffix(ks->font_metric));
      }
      else {
        if (ks->font_width>=0)
          fprintf(f,"%sfont width %f%s\n",sindent,ks->font_width,metric2suffix(ks->font_metric));
        if (ks->font_height>=0)
        fprintf(f,"%sfont height %f%s\n",sindent,ks->font_height,metric2suffix(ks->font_metric));
      }
    }
    if (ks->left_margin!=0 || ks->top_margin!=0 || ks->right_margin!=0 || ks->bottom_margin!=0) {
      const char *margin_metric=metric2suffix(ks->margin_metric);
      fprintf(
        f,"%smargins %f%s %f%s %f%s %f%s\n",
        sindent,
        ks->left_margin,margin_metric,ks->top_margin,margin_metric,
        ks->right_margin,margin_metric,ks->bottom_margin,margin_metric
      );
    }
    if (ks->bold) fprintf(f,"%sbold\n",sindent);
    if (ks->italics) fprintf(f,"%sitalics\n",sindent);
    if (ks->underline) fprintf(f,"%sunderline\n",sindent);
    if (ks->strike) fprintf(f,"%sstrike\n",sindent);
    if (ks->justify) fprintf(f,"%sjustify\n",sindent);
    fprintf(f,"%swrap %s\n",sindent,wrap2text(ks->wrap_mode));
    if (ks->meta) write_metadata(f,ks->meta,indent);
  }

  kate_free(sindent);
}
Пример #4
0
/*
* Fragment shader for surface color based on surface normals.
*/
static void fragment_shader_color(int y, int x, struct fragment_input *input, struct uniform_variables *vars){
    if(input->frag_coord[VAR_Z] >= read_depth(y, x)){
        return;
    }
    vec3 object_normal;
    object_normal[VAR_X] = input->attributes[0] / input->frag_coord[VAR_W];
    object_normal[VAR_Y] = input->attributes[1] / input->frag_coord[VAR_W];
    object_normal[VAR_Z] = input->attributes[2] / input->frag_coord[VAR_W];
    normalize_vec3(object_normal);
    if(input->front_facing == ER_FALSE){
        object_normal[VAR_X] = -object_normal[VAR_X];
        object_normal[VAR_Y] = -object_normal[VAR_Y];
        object_normal[VAR_Z] = -object_normal[VAR_Z];
    }
    float red, green, blue;
    red = (object_normal[VAR_X] * 0.5f + 0.5f);
    red = clamp(red, 0.0f, 1.0f);
    green = (object_normal[VAR_Y] * 0.5f + 0.5f);
    green = clamp(green, 0.0f, 1.0f);
    blue = (object_normal[VAR_X] * 0.5f + 0.5f);
    blue = clamp(blue, 0.0f, 1.0f);

    write_color(y, x, red, green, blue, 1.0f);
    write_depth(y, x, input->frag_coord[VAR_Z]);
}
Пример #5
0
/*
* Fragment shader for axis and arrows.
*/
static void fragment_shader_axis(int y, int x, struct fragment_input *input, struct uniform_variables *vars){
    if(input->frag_coord[VAR_Z] >= read_depth(y, x)){
        return;
    }
    write_color(y, x, input->attributes[0], input->attributes[1], input->attributes[2], 1.0f);
    write_depth(y, x, input->frag_coord[VAR_Z]);
}
Пример #6
0
bool PymolWriter::handle_point(PointGeometry *g, Color color,
                               std::string name) {
  setup(name, OTHER);
  write_color(get_stream(), color);
  get_stream() << "SPHERE, " << algebra::commas_io(g->get_geometry()) << ", "
               << .1 << ",\n";
  return true;
}
void
SaveColor(struct MyGadget *gd)
{
	write_integer(gd->data.col.cellsize);
	write_integer(gd->data.col.layout);
	write_integer(gd->data.col.redrawbuffer);

	write_color(&gd->data.col.value);
}
Пример #8
0
bool PymolWriter::handle_sphere(SphereGeometry *g, Color color,
                                std::string name) {
  setup(name, OTHER);
  write_color(get_stream(), color);
  get_stream() << "SPHERE, "
               << algebra::commas_io(g->get_geometry().get_center()) << ", "
               << g->get_geometry().get_radius() << ",\n";

  return true;
}
void
SaveSlider(struct MyGadget *gd)
{
	write_integer(gd->data.bslider.min);
	write_integer(gd->data.bslider.max);
	write_integer(gd->data.bslider.pos);
	write_integer(gd->data.bslider.style);
	write_integer(gd->data.bslider.hashtype);
	write_integer(gd->data.bslider.hashcount);
	write_integer(gd->data.bslider.thickness);
	write_integer(gd->data.bslider.keyinc);
	write_integer(gd->data.bslider.fillall);

	write_color(&gd->data.bslider.fillcolor);
	write_color(&gd->data.bslider.barcolor);

	write_string(&gd->data.bslider.Label[0]);
	write_string(&gd->data.bslider.MinLabel[0]);
	write_string(&gd->data.bslider.MaxLabel[0]);
}
void
SaveStatus(struct MyGadget *gd)
{
	write_integer(gd->data.sta.barheight);
	write_integer(gd->data.sta.maxvalue);
	write_integer(gd->data.sta.value);

	write_color(&gd->data.sta.barcolor);

	write_string(&gd->data.sta.Label[0]);
	write_string(&gd->data.sta.Trail[0]);
}
Пример #11
0
/*
* Fragment shader for surface lighting. One directional light source, phong shading.
*/
static void fragment_shader_light(int y, int x, struct fragment_input *input, struct uniform_variables *vars){

    if(input->frag_coord[VAR_Z] >= read_depth(y, x)){
        return;
    }
    float *light_dir = &(vars->uniform_float[0]);
    vec3 eye_normal, object_normal;
    object_normal[VAR_X] = input->attributes[0] / input->frag_coord[VAR_W];
    object_normal[VAR_Y] = input->attributes[1] / input->frag_coord[VAR_W];
    object_normal[VAR_Z] = input->attributes[2] / input->frag_coord[VAR_W];
    eye_normal[VAR_X] = input->attributes[3] / input->frag_coord[VAR_W];
    eye_normal[VAR_Y] = input->attributes[4] / input->frag_coord[VAR_W];
    eye_normal[VAR_Z] = input->attributes[5] / input->frag_coord[VAR_W];
    normalize_vec3(eye_normal);
    normalize_vec3(object_normal);
    if(input->front_facing == ER_FALSE){
        eye_normal[VAR_X] = -eye_normal[VAR_X];
        eye_normal[VAR_Y] = -eye_normal[VAR_Y];
        eye_normal[VAR_Z] = -eye_normal[VAR_Z];
        object_normal[VAR_X] = -object_normal[VAR_X];
        object_normal[VAR_Y] = -object_normal[VAR_Y];
        object_normal[VAR_Z] = -object_normal[VAR_Z];
    }
    float diffuse_term = max(0.0f, dot_vec3(light_dir, eye_normal) );
    float specular_term = 0.0f;
    vec3 view_vector;
    vec3 half_vector;
    view_vector[VAR_X] = 0.0f;
    view_vector[VAR_Y] = 0.0f;
    view_vector[VAR_Z] = 1.0f;
    if(diffuse_term > 0.0f){
        half_vector[VAR_X] = light_dir[VAR_X] + view_vector[VAR_X];
        half_vector[VAR_Y] = light_dir[VAR_Y] + view_vector[VAR_Y];
        half_vector[VAR_Z] = light_dir[VAR_Z] + view_vector[VAR_Z];
        normalize_vec3(half_vector);
        specular_term = pow( max(0.0f, dot_vec3(half_vector, eye_normal)), 50.0f );
    }

    float red, green, blue, light_intensity;
    light_intensity = 0.6f * diffuse_term + 0.4f * specular_term;
    red = (object_normal[VAR_X] * 0.5f + 0.5f) * light_intensity;
    red = clamp(red, 0.0f, 1.0f);
    green = (object_normal[VAR_Y] * 0.5f + 0.5f) * light_intensity;
    green = clamp(green, 0.0f, 1.0f);
    blue = (object_normal[VAR_Z] * 0.5f + 0.5f) * light_intensity;
    blue = clamp(blue, 0.0f, 1.0f);

    write_color(y, x, red, green, blue, 1.0f);
    write_depth(y, x, input->frag_coord[VAR_Z]);

}
Пример #12
0
/*
* Fragment shader for texture mapped cube.
*/
static void fs_cube(int y, int x, struct fragment_input *input, struct uniform_variables *vars){

    if(input->frag_coord[VAR_Z] >= read_depth(y, x)){
        return;
    }
    struct texture* tex = vars->uniform_texture[0];
    vec2 tex_coord;
    vec4 tex_color;
    tex_coord[VAR_S] = input->attributes[0] / input->frag_coord[VAR_W];
    tex_coord[VAR_T] = input->attributes[1] / input->frag_coord[VAR_W];
    texture_lod(tex, tex_coord, 0.0f, tex_color);

    write_color(y, x, tex_color[0], tex_color[1], tex_color[2], 1.0f);
    write_depth(y, x, input->frag_coord[VAR_Z]);
}
Пример #13
0
bool PymolWriter::handle_cylinder(CylinderGeometry *g, Color color,
                                  std::string name) {
  setup(name, OTHER);
  write_color(get_stream(), color);
  //  cleanup(name, false);
  get_stream() << "CYLINDER,\n"
               << algebra::commas_io(
                      g->get_geometry().get_segment().get_point(0)) << ",\n"
               << algebra::commas_io(g->get_geometry().get_segment().get_point(
                      1)) << ",\n" << g->get_geometry().get_radius() << ",\n";
  get_stream() << color.get_red() << ", " << color.get_green() << ", "
               << color.get_blue() << ",\n";
  get_stream() << color.get_red() << ", " << color.get_green() << ", "
               << color.get_blue() << ",\n";
  return true;
}
Пример #14
0
bool PymolWriter::handle_segment(SegmentGeometry *g, Color color,
                                 std::string name) {
  setup(name, LINES);
  /*double r= .01*(g->get_geometry().get_point(0)- g->get_geometry()
    .get_point(1)).get_magnitude();*/
  if (!open_type_) {
    get_stream() << "BEGIN, LINES,\n";
    open_type_ = LINES;
  }
  write_color(get_stream(), color);
  get_stream() << "VERTEX, "
               << algebra::commas_io(g->get_geometry().get_point(0)) << ",\n"
               << "VERTEX, "
               << algebra::commas_io(g->get_geometry().get_point(1)) << ",\n";
  //<< "END,\n";
  return true;
}
Пример #15
0
/*
* Fragment shader for texture mapped cube. Calculation of perspective correct derivatives for trilinear filtering.
*/
static void fs_cube_grad(int y, int x, struct fragment_input *input, struct uniform_variables *vars){
    
    if(input->frag_coord[VAR_Z] >= read_depth(y, x)){
        return;
    }

    struct texture *tex = vars->uniform_texture[0];
    vec2 tex_coord;
    vec4 tex_color;
    tex_coord[VAR_S] = input->attributes[0] / input->frag_coord[VAR_W];
    tex_coord[VAR_T] = input->attributes[1] / input->frag_coord[VAR_W];

    vec2 ddx, ddy;
    float one_over_w2 = 1.0f / (input->frag_coord[VAR_W] * input->frag_coord[VAR_W]);
    ddx[VAR_S] = (input->ddx[0] * input->frag_coord[VAR_W] - input->attributes[0] * input->dw_dx) * one_over_w2;
    ddx[VAR_T] = (input->ddx[1] * input->frag_coord[VAR_W] - input->attributes[1] * input->dw_dx) * one_over_w2;
    ddy[VAR_S] = (input->ddy[0] * input->frag_coord[VAR_W] - input->attributes[0] * input->dw_dy) * one_over_w2;
    ddy[VAR_T] = (input->ddy[1] * input->frag_coord[VAR_W] - input->attributes[1] * input->dw_dy) * one_over_w2;
    texture_grad(tex, tex_coord, ddx, ddy, tex_color);

    write_color(y, x, tex_color[0], tex_color[1], tex_color[2], 1.0f);
    write_depth(y, x, input->frag_coord[VAR_Z]);
}
Пример #16
0
/**
   set_color builtin
*/
static int builtin_set_color(parser_t &parser, wchar_t **argv)
{
    /** Variables used for parsing the argument list */
    const struct woption long_options[] =
    {
        { L"background", required_argument, 0, 'b'},
        { L"help", no_argument, 0, 'h' },
        { L"bold", no_argument, 0, 'o' },
        { L"underline", no_argument, 0, 'u' },
        { L"version", no_argument, 0, 'v' },
        { L"print-colors", no_argument, 0, 'c' },
        { 0, 0, 0, 0 }
    };

    const wchar_t *short_options = L"b:hvocu";

    int argc = builtin_count_args(argv);

    /* Some code passes variables to set_color that don't exist, like $fish_user_whatever. As a hack, quietly return failure. */
    if (argc <= 1)
    {
        return EXIT_FAILURE;
    }

    const wchar_t *bgcolor = NULL;
    bool bold = false, underline=false;
    int errret;

    /* Parse options to obtain the requested operation and the modifiers */
    woptind = 0;
    while (1)
    {
        int c = wgetopt_long(argc, argv, short_options, long_options, 0);

        if (c == -1)
        {
            break;
        }

        switch (c)
        {
            case 0:
                break;

            case 'b':
                bgcolor = woptarg;
                break;

            case 'h':
                builtin_print_help(parser, argv[0], stdout_buffer);
                return STATUS_BUILTIN_OK;

            case 'o':
                bold = true;
                break;

            case 'u':
                underline = true;
                break;

            case 'c':
                print_colors();
                return STATUS_BUILTIN_OK;

            case '?':
                return STATUS_BUILTIN_ERROR;
        }
    }

    /* Remaining argument is foreground color */
    const wchar_t *fgcolor = NULL;
    if (woptind < argc)
    {
        if (woptind + 1 == argc)
        {
            fgcolor = argv[woptind];
        }
        else
        {
            append_format(stderr_buffer,
                          _(L"%ls: Too many arguments\n"),
                          argv[0]);
            return STATUS_BUILTIN_ERROR;
        }
    }

    if (fgcolor == NULL && bgcolor == NULL && !bold && !underline)
    {
        append_format(stderr_buffer,
                      _(L"%ls: Expected an argument\n"),
                      argv[0]);
        return STATUS_BUILTIN_ERROR;
    }

    const rgb_color_t fg = rgb_color_t(fgcolor ? fgcolor : L"");
    if (fgcolor && (fg.is_none() || fg.is_ignore()))
    {
        append_format(stderr_buffer, _(L"%ls: Unknown color '%ls'\n"), argv[0], fgcolor);
        return STATUS_BUILTIN_ERROR;
    }

    const rgb_color_t bg = rgb_color_t(bgcolor ? bgcolor : L"");
    if (bgcolor && (bg.is_none() || bg.is_ignore()))
    {
        append_format(stderr_buffer, _(L"%ls: Unknown color '%ls'\n"), argv[0], bgcolor);
        return STATUS_BUILTIN_ERROR;
    }

    /* Make sure that the term exists */
    if (cur_term == NULL && setupterm(0, STDOUT_FILENO, &errret) == ERR)
    {
        append_format(stderr_buffer, _(L"%ls: Could not set up terminal\n"), argv[0]);
        return STATUS_BUILTIN_ERROR;
    }

    /*
       Test if we have at least basic support for setting fonts, colors
       and related bits - otherwise just give up...
    */
    if (! exit_attribute_mode)
    {
        return STATUS_BUILTIN_ERROR;
    }


    /* Save old output function so we can restore it */
    int (* const saved_writer_func)(char) = output_get_writer();

    /* Set our output function, which writes to a std::string */
    builtin_set_color_output.clear();
    output_set_writer(set_color_builtin_outputter);

    if (bold)
    {
        if (enter_bold_mode)
            writembs(tparm(enter_bold_mode));
    }

    if (underline)
    {
        if (enter_underline_mode)
            writembs(enter_underline_mode);
    }

    if (bgcolor != NULL)
    {
        if (bg.is_normal())
        {
            write_color(rgb_color_t::black(), false /* not is_fg */);
            writembs(tparm(exit_attribute_mode));
        }
    }

    if (fgcolor != NULL)
    {
        if (fg.is_normal() || fg.is_reset())
        {
            write_color(rgb_color_t::black(), true /* is_fg */);
            writembs(tparm(exit_attribute_mode));
        }
        else
        {
            write_color(fg, true /* is_fg */);
        }
    }

    if (bgcolor != NULL)
    {
        if (! bg.is_normal() && ! bg.is_reset())
        {
            write_color(bg, false /* not is_fg */);
        }
    }

    /* Restore saved writer function */
    output_set_writer(saved_writer_func);

    /* Output the collected string */
    stdout_buffer.append(str2wcstring(builtin_set_color_output));
    builtin_set_color_output.clear();

    return STATUS_BUILTIN_OK;
}
Пример #17
0
int main(int argc, char *argv[]) {
  int fd;              /* SPI device file descriptor */
  const int leds = 50; /* 50 LEDs in the strand */
  tcl_buffer buf;      /* Memory buffer for pixel values */
  int count;           /* Count of iterations (up to 3) */
  int i;               /* Counting Integer */

  /* Open SPI device */
  fd = open(device,O_WRONLY);
  if(fd<0) {
      /* Open failed */
      fprintf(stderr, "Error: SPI device open failed.\n");
      exit(1);
  }

  /* Initialize SPI bus for TCL pixels */
  if(spi_init(fd)<0) {
      /* Initialization failed */
      fprintf(stderr, "Unable to initialize SPI bus.\n");
      exit(1);
  }

  /* Allocate memory for the pixel buffer and initialize it */
  if(tcl_init(&buf,leds)<0) {
      /* Memory allocation failed */
      fprintf(stderr, "Insufficient memory for pixel buffer.\n");
      exit(1);
  }

  /* Loop Forever */
  while(1) {
    /* Do three iterations */
    for(count=0;count<3;count++) {
      /* Write color for every pixel */
      for(i=0;i<leds;i++) {
        if((i+count)%3==0) {
          /* Red pixel */
          write_color(&buf.pixels[i],255,0,0);
        }
        else if((i+count)%3==1) {
          /* Green pixel */
          write_color(&buf.pixels[i],0,255,0);
        }
        else {
          /* Blue pixel */
          write_color(&buf.pixels[i],0,0,255);
        }
      }

      /* Send the data to the TCL lighting strand */
      if(send_buffer(fd,&buf)<0) {
        fprintf(stderr, "Error sending data.\n");
        exit(1);
      }

      /* Sleep for 1 second */
      usleep(1000000);
    }
  }

  /* Although the program never gets to this point, below is how to clean up */

  /* Free the pixel buffer */
  tcl_free(&buf);

  /* Close the SPI device */
  close(fd);

  return 0;
}
Пример #18
0
int main(int argc, char* argv[]) {
  struct controller controller;
  glob_t pglob;

  char *device;
  glob("/sys/class/input/js*", 0, 0, &pglob);

  if (pglob.gl_pathc == 0) {
    sprintf(err, "Could not enumerate any input devices");
    return 0;
  }

  for (size_t i = 0; i < pglob.gl_pathc; ++i) {
    device = pglob.gl_pathv[i];
    if (!read_controller(device, &controller)) {
      fatal();
    }
    else {
      /* controller.capacity = 20; */
      float percent = controller.capacity / 100.f;
      printf("\ndevice: %s\n", device);
      printf("capacity: %d\n", controller.capacity);
      float scale = 255 * percent;
      printf("writing %f\n", scale);

      if (controller.capacity <= 10) {
        write_trigger(device, "heartbeat");
      }
      else {
        write_trigger(device, "none");
        write_color(device, "global", 1);
      }

      if (controller.capacity < 25) {
        if (!write_color(device, "red", 10)) fatal();
        if (!write_color(device, "green", 0)) fatal();
        if (!write_color(device, "blue", 0)) fatal();
      }
      else if (controller.capacity < 50) {
        if (!write_color(device, "red", 10)) fatal();
        if (!write_color(device, "green", 5)) fatal();
        if (!write_color(device, "blue", 0)) fatal();
      }
      else if (controller.capacity < 75 ) {
        if (!write_color(device, "red", 10)) fatal();
        if (!write_color(device, "green", 10)) fatal();
        if (!write_color(device, "blue", 0)) fatal();
      }
      else if (controller.capacity < 100 ) {
        if (!write_color(device, "red", 0)) fatal();
        if (!write_color(device, "green", 10)) fatal();
        if (!write_color(device, "blue", 0)) fatal();
      }
      else if (controller.capacity >= 100 ) {
        if (!write_color(device, "red", 0)) fatal();
        if (!write_color(device, "green", 0)) fatal();
        if (!write_color(device, "blue", 10)) fatal();
      }
    }
  }

  globfree(&pglob);

  return 0;

}
Пример #19
0
/// set_color builtin.
int builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
    wgetopter_t w;
    // Variables used for parsing the argument list.
    const struct woption long_options[] = {{L"background", required_argument, 0, 'b'},
                                           {L"help", no_argument, 0, 'h'},
                                           {L"bold", no_argument, 0, 'o'},
                                           {L"underline", no_argument, 0, 'u'},
                                           {L"version", no_argument, 0, 'v'},
                                           {L"print-colors", no_argument, 0, 'c'},
                                           {0, 0, 0, 0}};

    const wchar_t *short_options = L"b:hvocu";

    int argc = builtin_count_args(argv);

    // Some code passes variables to set_color that don't exist, like $fish_user_whatever. As a
    // hack, quietly return failure.
    if (argc <= 1) {
        return EXIT_FAILURE;
    }

    const wchar_t *bgcolor = NULL;
    bool bold = false, underline = false;
    int errret;

    // Parse options to obtain the requested operation and the modifiers.
    w.woptind = 0;
    while (1) {
        int c = w.wgetopt_long(argc, argv, short_options, long_options, 0);

        if (c == -1) {
            break;
        }

        switch (c) {
            case 0: {
                break;
            }
            case 'b': {
                bgcolor = w.woptarg;
                break;
            }
            case 'h': {
                builtin_print_help(parser, streams, argv[0], streams.out);
                return STATUS_BUILTIN_OK;
            }
            case 'o': {
                bold = true;
                break;
            }
            case 'u': {
                underline = true;
                break;
            }
            case 'c': {
                print_colors(streams);
                return STATUS_BUILTIN_OK;
            }
            case '?': {
                return STATUS_BUILTIN_ERROR;
            }
        }
    }

    // Remaining arguments are foreground color.
    std::vector<rgb_color_t> fgcolors;
    for (; w.woptind < argc; w.woptind++) {
        rgb_color_t fg = rgb_color_t(argv[w.woptind]);
        if (fg.is_none()) {
            streams.err.append_format(_(L"%ls: Unknown color '%ls'\n"), argv[0], argv[w.woptind]);
            return STATUS_BUILTIN_ERROR;
        }
        fgcolors.push_back(fg);
    }

    if (fgcolors.empty() && bgcolor == NULL && !bold && !underline) {
        streams.err.append_format(_(L"%ls: Expected an argument\n"), argv[0]);
        return STATUS_BUILTIN_ERROR;
    }

    // #1323: We may have multiple foreground colors. Choose the best one. If we had no foreground
    // color, we'll get none(); if we have at least one we expect not-none.
    const rgb_color_t fg = best_color(fgcolors, output_get_color_support());
    assert(fgcolors.empty() || !fg.is_none());

    const rgb_color_t bg = rgb_color_t(bgcolor ? bgcolor : L"");
    if (bgcolor && bg.is_none()) {
        streams.err.append_format(_(L"%ls: Unknown color '%ls'\n"), argv[0], bgcolor);
        return STATUS_BUILTIN_ERROR;
    }

    // Make sure that the term exists.
    if (cur_term == NULL && setupterm(0, STDOUT_FILENO, &errret) == ERR) {
        streams.err.append_format(_(L"%ls: Could not set up terminal\n"), argv[0]);
        return STATUS_BUILTIN_ERROR;
    }

    // Test if we have at least basic support for setting fonts, colors and related bits - otherwise
    // just give up...
    if (!exit_attribute_mode) {
        return STATUS_BUILTIN_ERROR;
    }

    // Save old output function so we can restore it.
    int (*const saved_writer_func)(char) = output_get_writer();

    // Set our output function, which writes to a std::string.
    builtin_set_color_output.clear();
    output_set_writer(set_color_builtin_outputter);

    if (bold) {
        if (enter_bold_mode) writembs(tparm(enter_bold_mode));
    }

    if (underline) {
        if (enter_underline_mode) writembs(enter_underline_mode);
    }

    if (bgcolor != NULL) {
        if (bg.is_normal()) {
            write_color(rgb_color_t::black(), false /* not is_fg */);
            writembs(tparm(exit_attribute_mode));
        }
    }

    if (!fg.is_none()) {
        if (fg.is_normal() || fg.is_reset()) {
            write_color(rgb_color_t::black(), true /* is_fg */);
            writembs(tparm(exit_attribute_mode));
        } else {
            write_color(fg, true /* is_fg */);
        }
    }

    if (bgcolor != NULL) {
        if (!bg.is_normal() && !bg.is_reset()) {
            write_color(bg, false /* not is_fg */);
        }
    }

    // Restore saved writer function.
    output_set_writer(saved_writer_func);

    // Output the collected string.
    streams.out.append(str2wcstring(builtin_set_color_output));
    builtin_set_color_output.clear();

    return STATUS_BUILTIN_OK;
}