Repository: typst/typst
File: crates/typst-pdf/src/gradient.rs (Rust)

Code before:

                    color::write(
                        color_space,
                        stream_shading.color_space(),
                        &context.globals.color_functions,
                    );

                    let range = color_space.range();
                    stream_shading
                        .bits_per_coordinate(16)
                        .bits_per_component(16)
                        .bits_per_flag(8)
                        .shading_type(StreamShadingType::CoonsPatch)
                        .decode(match color_space {
                            ColorSpace::Cmyk => SmallVec::<[f32; 12]>::from_buf([
                                0.0, 1.0, 0.0, 1.0, range[0], range[1], range[2],
                                range[3], range[4], range[5], range[6], range[7],
                            ]),
                            ColorSpace::D65Gray => {
                                smallvec![0.0, 1.0, 0.0, 1.0, range[0], range[1]]
                            }
                            _ => smallvec![
                                0.0, 1.0, 0.0, 1.0, range[0], range[1], range[2],
                                range[3], range[4], range[5],
                            ],
                        })
                        .anti_alias(gradient.anti_alias())
                        .filter(Filter::FlateDecode);

                    stream_shading.finish();

                    let mut shading_pattern = chunk.shading_pattern(shading);
                    shading_pattern.shading_ref(stream_shading_id);
                    shading_pattern
                }
            };

            shading_pattern.matrix(transform_to_array(*transform));
        }
    });

    (chunk, out)
}

/// Writes an expotential or stitched function that expresses the gradient.
fn shading_function(
    gradient: &Gradient,
    chunk: &mut PdfChunk,
    color_space: ColorSpace,
) -> Ref {
    let function = chunk.alloc();

Review comment (refactor):
`decode` takes an `IntoIterator<Item = f32>`, so you can simplify this a lot by just appending the range, which already only contains the correct number of components:
```rs
.decode([0.0, 1.0, 0.0, 1.0].into_iter().chain(range))
```

Code after:
                    color::write(
                        color_space,
                        stream_shading.color_space(),
                        &context.globals.color_functions,
                    );

                    let range = color_space.range();
                    stream_shading
                        .bits_per_coordinate(16)
                        .bits_per_component(16)
                        .bits_per_flag(8)
                        .shading_type(StreamShadingType::CoonsPatch)
                        .decode(
                            [0.0, 1.0, 0.0, 1.0].into_iter().chain(range.iter().copied()),
                        )
                        .anti_alias(gradient.anti_alias())
                        .filter(Filter::FlateDecode);

                    stream_shading.finish();

                    let mut shading_pattern = chunk.shading_pattern(shading);
                    shading_pattern.shading_ref(stream_shading_id);
                    shading_pattern
                }
            };

            shading_pattern.matrix(transform_to_array(*transform));
        }
    });

    (chunk, out)
}

/// Writes an expotential or stitched function that expresses the gradient.
fn shading_function(
    gradient: &Gradient,
    chunk: &mut PdfChunk,
    color_space: ColorSpace,
) -> Ref {
    let function = chunk.alloc();
    let mut functions = vec![];
    let mut bounds = vec![];
    let mut encode = vec![];

    // Create the individual gradient functions for each pair of stops.
    for window in gradient.stops_ref().windows(2) {
        let (first, second) = (window[0], window[1]);

        // If we have a hue index, we will create several stops in-between
        // to make the gradient smoother without interpolation issues with
        // native color spaces.