When programming i have encountered a simple ( i think ) problem, yet i can't get my head around it. I am trying to convert a string ( like "#FF00FF" for magenta ) into a color. I figured out that i need to skip the first character '#' and then using "chunks" range convert each pair of 2 chars into a number and assign it to specific component. The snippet looks like this

        Color color;
        auto chunk = chunks( str[1..$], 2 );
        color.r = to!ubyte( chunk.front, 16 ); chunk.popFront;
        color.g = to!ubyte( chunk.front, 16 ); chunk.popFront;
        color.b = to!ubyte( chunk.front, 16 ); chunk.popFront;

But the compilation fails, stating

/usr/include/dlang/dmd/std/conv.d(295): Error: template std.conv.toImpl cannot deduce function from argument types !(ubyte)(Take!string, int), candidates are: /usr/include/dlang/dmd/std/conv.d(361): std.conv.toImpl(T, S)(S value) if (isImplicitlyConvertible!(S, T) && !isEnumStrToStr!(S, T) && !isNullToStr!(S, T)) /usr/include/dlang/dmd/std/conv.d(475): std.conv.toImpl(T, S)(ref S s) if (isRawStaticArray!S) /usr/include/dlang/dmd/std/conv.d(491): std.conv.toImpl(T, S)(S value) if (!isImplicitlyConvertible!(S, T) && is(typeof(S.init.opCast!T()) : T) && !isExactSomeString!T && !is(typeof(T(value)))) /usr/include/dlang/dmd/std/conv.d(542): std.conv.toImpl(T, S)(S value) if (!isImplicitlyConvertible!(S, T) && is(T == struct) && is(typeof(T(value)))) /usr/include/dlang/dmd/std/conv.d(591): std.conv.toImpl(T, S)(S value) if (!isImplicitlyConvertible!(S, T) && is(T == class) && is(typeof(new T(value)))) /usr/include/dlang/dmd/std/conv.d(295): ... (9 more, -v to show) ... source/engine/graphics/core.d(43): Error: template instance std.conv.to!ubyte.to!(Take!string, int) error instantiating

I would really love to solve this problem using ranges, because i am learning how to use them. Unfortunately even such a simple task seems so hard for me ;(

Reply via email to